Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for positional args in graphql/execute/subscribe func #2301

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/__tests__/starWarsQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ describe('Star Wars Query Tests', () => {
});
});

it('Accepts positional arguments to graphql()', async () => {
const source = `
query HeroNameQuery {
hero {
name
}
}
`;

const result = await graphql(schema, source);
expect(result).to.deep.equal({
data: {
hero: {
name: 'R2-D2',
},
},
});
});

it('Allows us to query for the ID and friends of R2-D2', async () => {
const source = `
query HeroNameAndFriendsQuery {
Expand Down
24 changes: 0 additions & 24 deletions src/execution/__tests__/executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,6 @@ describe('Execute: Handles basic execution tasks', () => {
);
});

it('accepts positional arguments', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Type',
fields: {
a: {
type: GraphQLString,
resolve(rootValue) {
return rootValue;
},
},
},
}),
});

const document = parse('{ a }');
const rootValue = 'rootValue';
const result = execute(schema, document, rootValue);

expect(result).to.deep.equal({
data: { a: 'rootValue' },
});
});

it('executes arbitrary code', async () => {
const data = {
a: () => 'Apple',
Expand Down
43 changes: 1 addition & 42 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,48 +139,7 @@ export type ExecutionArgs = {|
*
* Accepts either an object with named arguments, or individual arguments.
*/
declare function execute(
ExecutionArgs,
..._: []
): PromiseOrValue<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function execute(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{ +[variable: string]: mixed, ... },
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
typeResolver?: ?GraphQLTypeResolver<any, any>,
): PromiseOrValue<ExecutionResult>;
export function execute(
argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
) {
/* eslint-enable no-redeclare */
// Extract arguments from object args if provided.
return arguments.length === 1
? executeImpl(argsOrSchema)
: executeImpl({
schema: argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
});
}

function executeImpl(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
const {
schema,
document,
Expand Down
82 changes: 5 additions & 77 deletions src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,10 @@ export type GraphQLArgs = {|
fieldResolver?: ?GraphQLFieldResolver<any, any>,
typeResolver?: ?GraphQLTypeResolver<any, any>,
|};
declare function graphql(GraphQLArgs, ..._: []): Promise<ExecutionResult>;
/* eslint-disable no-redeclare */
declare function graphql(
schema: GraphQLSchema,
source: Source | string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{ +[variable: string]: mixed, ... },
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
typeResolver?: ?GraphQLTypeResolver<any, any>,
): Promise<ExecutionResult>;
export function graphql(
argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
) {
/* eslint-enable no-redeclare */

export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {
// Always return a Promise for a consistent API.
return new Promise(resolve =>
resolve(
// Extract arguments from object args if provided.
arguments.length === 1
? graphqlImpl(argsOrSchema)
: graphqlImpl({
schema: argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
}),
),
);
return new Promise(resolve => resolve(graphqlImpl(args)));
}

/**
Expand All @@ -115,43 +78,8 @@ export function graphql(
* However, it guarantees to complete synchronously (or throw an error) assuming
* that all field resolvers are also synchronous.
*/
declare function graphqlSync(GraphQLArgs, ..._: []): ExecutionResult;
/* eslint-disable no-redeclare */
declare function graphqlSync(
schema: GraphQLSchema,
source: Source | string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{ +[variable: string]: mixed, ... },
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
typeResolver?: ?GraphQLTypeResolver<any, any>,
): ExecutionResult;
export function graphqlSync(
argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
) {
/* eslint-enable no-redeclare */
// Extract arguments from object args if provided.
const result =
arguments.length === 1
? graphqlImpl(argsOrSchema)
: graphqlImpl({
schema: argsOrSchema,
source,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
});
export function graphqlSync(args: GraphQLArgs): ExecutionResult {
const result = graphqlImpl(args);

// Assert that the execution was synchronous.
if (isPromise(result)) {
Expand Down
31 changes: 0 additions & 31 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,6 @@ async function expectPromiseToThrow(promise, message) {

// Check all error cases when initializing the subscription.
describe('Subscription Initialization Phase', () => {
it('accepts positional arguments', async () => {
const document = parse(`
subscription {
importantEmail
}
`);

async function* emptyAsyncIterator() {
// Empty
}

const ai = await subscribe(emailSchema, document, {
importantEmail: emptyAsyncIterator,
});

// $FlowFixMe
ai.return();
});

it('accepts multiple subscription fields defined in schema', async () => {
const pubsub = new EventEmitter();
const SubscriptionTypeMultiple = new GraphQLObjectType({
Expand Down Expand Up @@ -320,12 +301,6 @@ describe('Subscription Initialization Phase', () => {
}
`);

await expectPromiseToThrow(
// $DisableFlowOnNegativeTest
() => subscribe(null, document),
'Expected null to be a GraphQL schema.',
);

await expectPromiseToThrow(
// $DisableFlowOnNegativeTest
() => subscribe({ document }),
Expand All @@ -334,12 +309,6 @@ describe('Subscription Initialization Phase', () => {
});

it('throws an error if document is missing', async () => {
await expectPromiseToThrow(
// $DisableFlowOnNegativeTest
() => subscribe(emailSchema, null),
'Must provide document',
);

await expectPromiseToThrow(
// $DisableFlowOnNegativeTest
() => subscribe({ schema: emailSchema }),
Expand Down
65 changes: 12 additions & 53 deletions src/subscription/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,60 +60,7 @@ export type SubscriptionArgs = {|
*
* Accepts either an object with named arguments, or individual arguments.
*/
declare function subscribe(
SubscriptionArgs,
..._: []
): Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
/* eslint-disable no-redeclare */
declare function subscribe(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{ +[variable: string]: mixed, ... },
operationName?: ?string,
fieldResolver?: ?GraphQLFieldResolver<any, any>,
subscribeFieldResolver?: ?GraphQLFieldResolver<any, any>,
): Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
export function subscribe(
argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
) {
/* eslint-enable no-redeclare */
// Extract arguments from object args if provided.
return arguments.length === 1
? subscribeImpl(argsOrSchema)
: subscribeImpl({
schema: argsOrSchema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
subscribeFieldResolver,
});
}

/**
* This function checks if the error is a GraphQLError. If it is, report it as
* an ExecutionResult, containing only errors and no data. Otherwise treat the
* error as a system-class error and re-throw it.
*/
function reportGraphQLError(error) {
if (error instanceof GraphQLError) {
return { errors: [error] };
}
throw error;
}

function subscribeImpl(
args: SubscriptionArgs,
): Promise<AsyncIterator<ExecutionResult> | ExecutionResult> {
const {
Expand Down Expand Up @@ -168,6 +115,18 @@ function subscribeImpl(
);
}

/**
* This function checks if the error is a GraphQLError. If it is, report it as
* an ExecutionResult, containing only errors and no data. Otherwise treat the
* error as a system-class error and re-throw it.
*/
function reportGraphQLError(error) {
if (error instanceof GraphQLError) {
return { errors: [error] };
}
throw error;
}

/**
* Implements the "CreateSourceEventStream" algorithm described in the
* GraphQL specification, resolving the subscription source event stream.
Expand Down