Skip to content

Commit

Permalink
fix: support query variable binding (#6)
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
vedmalex authored and gregberge committed Feb 22, 2018
1 parent 28cb5b9 commit fead242
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/addDirectiveResolveFunctionsToSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@ 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.`,
)
}

const resolver = resolverMap[name]
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 }
}

Expand All @@ -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))
}

Expand All @@ -83,6 +82,7 @@ function createFieldResolver(field, resolverMap, schema) {
resolverMap,
schema,
DirectiveLocation.FIELD,
info.variableValues,
)
return () =>
directiveInfo.resolver(
Expand Down
16 changes: 14 additions & 2 deletions src/addDirectiveResolveFunctionsToSchema.test.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('addDirectiveResolveFunctionsToSchema', () => {
getFieldName(resolve, source, directiveArgs, context, info) {
return info.fieldName
},

}

schema = makeExecutableSchema({ typeDefs, resolvers })
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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' })
})
})
})

0 comments on commit fead242

Please sign in to comment.