From 6719b1b9fcb6b3bd82ea011e532cc56ead36cb74 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 20 Dec 2019 13:32:55 +0700 Subject: [PATCH] Remove support for positional args in graphql/execute/subscribe func Note: It's a big breaking change and is intended to be released only in v16.x.x line --- src/__tests__/starWarsQuery-test.js | 19 ----- src/execution/__tests__/executor-test.js | 24 ------ src/execution/execute.js | 43 +--------- src/graphql.js | 82 ++------------------ src/subscription/__tests__/subscribe-test.js | 31 -------- src/subscription/subscribe.js | 65 +++------------- 6 files changed, 18 insertions(+), 246 deletions(-) diff --git a/src/__tests__/starWarsQuery-test.js b/src/__tests__/starWarsQuery-test.js index 64ecdf1368..651c8f9cad 100644 --- a/src/__tests__/starWarsQuery-test.js +++ b/src/__tests__/starWarsQuery-test.js @@ -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 { diff --git a/src/execution/__tests__/executor-test.js b/src/execution/__tests__/executor-test.js index 4d2fdab48f..5947d710f6 100644 --- a/src/execution/__tests__/executor-test.js +++ b/src/execution/__tests__/executor-test.js @@ -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', diff --git a/src/execution/execute.js b/src/execution/execute.js index d13590d261..0cf29e9b9a 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -139,48 +139,7 @@ export type ExecutionArgs = {| * * Accepts either an object with named arguments, or individual arguments. */ -declare function execute( - ExecutionArgs, - ..._: [] -): PromiseOrValue; -/* eslint-disable no-redeclare */ -declare function execute( - schema: GraphQLSchema, - document: DocumentNode, - rootValue?: mixed, - contextValue?: mixed, - variableValues?: ?{ +[variable: string]: mixed, ... }, - operationName?: ?string, - fieldResolver?: ?GraphQLFieldResolver, - typeResolver?: ?GraphQLTypeResolver, -): PromiseOrValue; -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 { +export function execute(args: ExecutionArgs): PromiseOrValue { const { schema, document, diff --git a/src/graphql.js b/src/graphql.js index a49f032166..1846452ec9 100644 --- a/src/graphql.js +++ b/src/graphql.js @@ -66,47 +66,10 @@ export type GraphQLArgs = {| fieldResolver?: ?GraphQLFieldResolver, typeResolver?: ?GraphQLTypeResolver, |}; -declare function graphql(GraphQLArgs, ..._: []): Promise; -/* eslint-disable no-redeclare */ -declare function graphql( - schema: GraphQLSchema, - source: Source | string, - rootValue?: mixed, - contextValue?: mixed, - variableValues?: ?{ +[variable: string]: mixed, ... }, - operationName?: ?string, - fieldResolver?: ?GraphQLFieldResolver, - typeResolver?: ?GraphQLTypeResolver, -): Promise; -export function graphql( - argsOrSchema, - source, - rootValue, - contextValue, - variableValues, - operationName, - fieldResolver, - typeResolver, -) { - /* eslint-enable no-redeclare */ + +export function graphql(args: GraphQLArgs): Promise { // 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))); } /** @@ -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, - typeResolver?: ?GraphQLTypeResolver, -): 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)) { diff --git a/src/subscription/__tests__/subscribe-test.js b/src/subscription/__tests__/subscribe-test.js index 94d7766cc3..f25005f858 100644 --- a/src/subscription/__tests__/subscribe-test.js +++ b/src/subscription/__tests__/subscribe-test.js @@ -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({ @@ -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 }), @@ -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 }), diff --git a/src/subscription/subscribe.js b/src/subscription/subscribe.js index 5252532675..8f9f6a9608 100644 --- a/src/subscription/subscribe.js +++ b/src/subscription/subscribe.js @@ -60,60 +60,7 @@ export type SubscriptionArgs = {| * * Accepts either an object with named arguments, or individual arguments. */ -declare function subscribe( - SubscriptionArgs, - ..._: [] -): Promise | ExecutionResult>; -/* eslint-disable no-redeclare */ -declare function subscribe( - schema: GraphQLSchema, - document: DocumentNode, - rootValue?: mixed, - contextValue?: mixed, - variableValues?: ?{ +[variable: string]: mixed, ... }, - operationName?: ?string, - fieldResolver?: ?GraphQLFieldResolver, - subscribeFieldResolver?: ?GraphQLFieldResolver, -): Promise | 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 | ExecutionResult> { const { @@ -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.