Skip to content

Commit fead242

Browse files
vedmalexgregberge
authored andcommitted
fix: support query variable binding (#6)
Closes #5
1 parent 28cb5b9 commit fead242

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/addDirectiveResolveFunctionsToSchema.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ function createAsyncResolver(field) {
2020
originalResolver(source, args, context, info)
2121
}
2222

23-
function getDirectiveInfo(directive, resolverMap, schema, location) {
23+
function getDirectiveInfo(directive, resolverMap, schema, location, variables) {
2424
const name = directive.name.value
2525

2626
const Directive = schema.getDirective(name)
2727
if (typeof Directive === 'undefined') {
2828
throw new Error(
2929
`Directive @${name} is undefined. ` +
30-
'Please define in schema before using.',
30+
'Please define in schema before using.',
3131
)
3232
}
3333

3434
if (!Directive.locations.includes(location)) {
3535
throw new Error(
3636
`Directive @${name} is not marked to be used on "${location}" location. ` +
37-
`Please add "directive @${name} ON ${location}" in schema.`,
37+
`Please add "directive @${name} ON ${location}" in schema.`,
3838
)
3939
}
4040

4141
const resolver = resolverMap[name]
4242
if (!resolver && !BUILT_IN_DIRECTIVES.includes(name)) {
4343
throw new Error(
4444
`Directive @${name} has no resolver.` +
45-
'Please define one using createFieldExecutionResolver().',
45+
'Please define one using createFieldExecutionResolver().',
4646
)
4747
}
4848

49-
const args = getDirectiveValues(Directive, { directives: [directive] })
49+
const args = getDirectiveValues(Directive, { directives: [directive] }, variables)
5050
return { args, resolver }
5151
}
5252

@@ -60,14 +60,13 @@ function createFieldExecutionResolver(field, resolverMap, schema) {
6060
schema,
6161
DirectiveLocation.FIELD_DEFINITION,
6262
)
63-
return (source, args, context, info) =>
64-
directiveInfo.resolver(
65-
() => recursiveResolver(source, args, context, info),
66-
source,
67-
directiveInfo.args,
68-
context,
69-
info,
70-
)
63+
return (source, args, context, info) => directiveInfo.resolver(
64+
() => recursiveResolver(source, args, context, info),
65+
source,
66+
directiveInfo.args,
67+
context,
68+
info,
69+
)
7170
}, createAsyncResolver(field))
7271
}
7372

@@ -83,6 +82,7 @@ function createFieldResolver(field, resolverMap, schema) {
8382
resolverMap,
8483
schema,
8584
DirectiveLocation.FIELD,
85+
info.variableValues,
8686
)
8787
return () =>
8888
directiveInfo.resolver(

src/addDirectiveResolveFunctionsToSchema.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* eslint-disable no-shadow */
2+
import url from 'url';
23
import { makeExecutableSchema } from 'graphql-tools'
34
import { graphql } from 'graphql'
45
import { addDirectiveResolveFunctionsToSchema } from './'
56

6-
const run = async (schema, query, context) => {
7-
const { data, errors } = await graphql(schema, query, null, context)
7+
const run = async (schema, query, context, variables) => {
8+
const { data, errors } = await graphql(schema, query, null, context, variables)
89
if (errors && errors.length) {
910
/* eslint-disable no-console */
1011
console.error(errors)
@@ -77,6 +78,7 @@ describe('addDirectiveResolveFunctionsToSchema', () => {
7778
getFieldName(resolve, source, directiveArgs, context, info) {
7879
return info.fieldName
7980
},
81+
8082
}
8183

8284
schema = makeExecutableSchema({ typeDefs, resolvers })
@@ -260,6 +262,7 @@ describe('addDirectiveResolveFunctionsToSchema', () => {
260262
directive @prefixWithId on FIELD
261263
directive @getContextKey(key: String!) on FIELD
262264
directive @getFieldName on FIELD
265+
directive @url(root: String!) on FIELD
263266
264267
type Query {
265268
foo: String
@@ -304,6 +307,9 @@ describe('addDirectiveResolveFunctionsToSchema', () => {
304307
getFieldName(resolve, source, directiveArgs, context, info) {
305308
return info.fieldName
306309
},
310+
async url(resolve, source, directiveArgs) {
311+
return url.resolve(directiveArgs.root, await resolve())
312+
},
307313
}
308314

309315
schema = makeExecutableSchema({ typeDefs, resolvers })
@@ -391,5 +397,11 @@ describe('addDirectiveResolveFunctionsToSchema', () => {
391397
const data = await run(schema, query)
392398
expect(data).toEqual({ foo: 'foo' })
393399
})
400+
401+
it('should support query variables binding', async () => {
402+
const query = /* GraphQL */ `query($url: String!) { foo @url(root:$url) }`
403+
const data = await run(schema, query, null, { url: '/root/url/' })
404+
expect(data).toEqual({ foo: '/root/url/foo' })
405+
})
394406
})
395407
})

0 commit comments

Comments
 (0)