From fead242777c687d57ba087cf18a1208d3c22f78c Mon Sep 17 00:00:00 2001 From: "Vedanta-krit das (Alex Vedmedenko)" Date: Thu, 22 Feb 2018 14:16:04 +0300 Subject: [PATCH] fix: support query variable binding (#6) Closes #5 --- src/addDirectiveResolveFunctionsToSchema.js | 26 +++++++++---------- ...dDirectiveResolveFunctionsToSchema.test.js | 16 ++++++++++-- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/addDirectiveResolveFunctionsToSchema.js b/src/addDirectiveResolveFunctionsToSchema.js index a9bd957..529b5b8 100644 --- a/src/addDirectiveResolveFunctionsToSchema.js +++ b/src/addDirectiveResolveFunctionsToSchema.js @@ -20,21 +20,21 @@ function createAsyncResolver(field) { originalResolver(source, args, context, info) } -function getDirectiveInfo(directive, resolverMap, schema, location) { +function getDirectiveInfo(directive, resolverMap, schema, location, variables) { const name = directive.name.value const Directive = schema.getDirective(name) if (typeof Directive === 'undefined') { throw new Error( `Directive @${name} is undefined. ` + - 'Please define in schema before using.', + 'Please define in schema before using.', ) } if (!Directive.locations.includes(location)) { throw new Error( `Directive @${name} is not marked to be used on "${location}" location. ` + - `Please add "directive @${name} ON ${location}" in schema.`, + `Please add "directive @${name} ON ${location}" in schema.`, ) } @@ -42,11 +42,11 @@ function getDirectiveInfo(directive, resolverMap, schema, location) { if (!resolver && !BUILT_IN_DIRECTIVES.includes(name)) { throw new Error( `Directive @${name} has no resolver.` + - 'Please define one using createFieldExecutionResolver().', + 'Please define one using createFieldExecutionResolver().', ) } - const args = getDirectiveValues(Directive, { directives: [directive] }) + const args = getDirectiveValues(Directive, { directives: [directive] }, variables) return { args, resolver } } @@ -60,14 +60,13 @@ function createFieldExecutionResolver(field, resolverMap, schema) { schema, DirectiveLocation.FIELD_DEFINITION, ) - return (source, args, context, info) => - directiveInfo.resolver( - () => recursiveResolver(source, args, context, info), - source, - directiveInfo.args, - context, - info, - ) + return (source, args, context, info) => directiveInfo.resolver( + () => recursiveResolver(source, args, context, info), + source, + directiveInfo.args, + context, + info, + ) }, createAsyncResolver(field)) } @@ -83,6 +82,7 @@ function createFieldResolver(field, resolverMap, schema) { resolverMap, schema, DirectiveLocation.FIELD, + info.variableValues, ) return () => directiveInfo.resolver( diff --git a/src/addDirectiveResolveFunctionsToSchema.test.js b/src/addDirectiveResolveFunctionsToSchema.test.js index c728b5e..a03bdac 100644 --- a/src/addDirectiveResolveFunctionsToSchema.test.js +++ b/src/addDirectiveResolveFunctionsToSchema.test.js @@ -1,10 +1,11 @@ /* eslint-disable no-shadow */ +import url from 'url'; import { makeExecutableSchema } from 'graphql-tools' import { graphql } from 'graphql' import { addDirectiveResolveFunctionsToSchema } from './' -const run = async (schema, query, context) => { - const { data, errors } = await graphql(schema, query, null, context) +const run = async (schema, query, context, variables) => { + const { data, errors } = await graphql(schema, query, null, context, variables) if (errors && errors.length) { /* eslint-disable no-console */ console.error(errors) @@ -77,6 +78,7 @@ describe('addDirectiveResolveFunctionsToSchema', () => { getFieldName(resolve, source, directiveArgs, context, info) { return info.fieldName }, + } schema = makeExecutableSchema({ typeDefs, resolvers }) @@ -260,6 +262,7 @@ describe('addDirectiveResolveFunctionsToSchema', () => { directive @prefixWithId on FIELD directive @getContextKey(key: String!) on FIELD directive @getFieldName on FIELD + directive @url(root: String!) on FIELD type Query { foo: String @@ -304,6 +307,9 @@ describe('addDirectiveResolveFunctionsToSchema', () => { getFieldName(resolve, source, directiveArgs, context, info) { return info.fieldName }, + async url(resolve, source, directiveArgs) { + return url.resolve(directiveArgs.root, await resolve()) + }, } schema = makeExecutableSchema({ typeDefs, resolvers }) @@ -391,5 +397,11 @@ describe('addDirectiveResolveFunctionsToSchema', () => { const data = await run(schema, query) expect(data).toEqual({ foo: 'foo' }) }) + + it('should support query variables binding', async () => { + const query = /* GraphQL */ `query($url: String!) { foo @url(root:$url) }` + const data = await run(schema, query, null, { url: '/root/url/' }) + expect(data).toEqual({ foo: '/root/url/foo' }) + }) }) })