Skip to content

Allow non-scalar directive arguments and variables #4

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 30 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GraphQLDirective} from 'graphql/type/directives';
import {GraphQLSchema, parse} from 'graphql';
import {GraphQLSchema, getDirectiveValues} from 'graphql';

const DEFAULT_DIRECTIVES = ['skip', 'include'];

Expand Down Expand Up @@ -28,20 +28,22 @@ function resolveWithDirective(resolve, source, directive, context, info) {
d => directive.name.value === d.name,
)[0];

let args = {};

for (let arg of directive.arguments) {
args[arg.name.value] = arg.value.value;
}
const args = getDirectiveValues(
directiveConfig,
{
directives: [directive],
},
info.variableValues
);

return directiveConfig.resolve(resolve, source, args, context, info);
}

/**
* parse directives from a schema defenition form them as graphql directive structure
* parse directives from a schema definition into directive AST nodes
*/
function parseSchemaDirectives(directives) {
let schemaDirectives = [];
let directiveNodes = [];

if (
!directives ||
Expand All @@ -52,22 +54,30 @@ function parseSchemaDirectives(directives) {
}

for (let directiveName in directives) {
let argsList = [],
args = '';

Object.keys(directives[directiveName]).map(key => {
argsList.push(`${key}:"${directives[directiveName][key]}"`);
});

if (argsList.length > 0) {
args = `(${argsList.join(',')})`;
let directiveArgNodes = [];
for (let argName in directives.args || []) {
const arg = directives.args[argName];
directiveArgNodes.push({
kind: 'InputValueDefinition',
description: arg.description,
name: arg.name,
type: arg.type,
defaultValue: arg.defaultValue,
});
}

schemaDirectives.push(`@${directiveName}${args}`);
directiveNodes.push({
kind: 'DirectiveDefinition',
description: directives[directiveName].description,
name: {
kind: 'Name',
value: directiveName,
},
arguments: directiveArgNodes,
});
}

return parse(`{ a: String ${schemaDirectives.join(' ')} }`).definitions[0]
.selectionSet.selections[0].directives;
return directiveNodes;
}

/**
Expand Down
76 changes: 61 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
} from '../src/index';
import {
GraphQLInt,
GraphQLInputObjectType,
GraphQLSchema,
GraphQLObjectType,
GraphQLNonNull,
GraphQLList,
graphql,
Expand All @@ -21,14 +21,34 @@ import {

import {expect} from 'chai';

const TestOption = new GraphQLInputObjectType({
name: 'TestOption',
fields: {
againBy: {
type: GraphQLInt,
description: 'then duplicate again this many times',
},
}
});

let GraphQLTestDirective,
GraphQLTestDirectiveTrows,
GraphQLTestDirectiveThrows,
GraphQLTestDirectiveCatch,
errors,
schema;

describe('GraphQLCustomDirective', () => {
before(() => {
function doDuplicate(input, by) {
let times = [];

for (let i = 0; i < (by || 2); i++) {
times.push(input);
}

return times.join(' ');
}

GraphQLTestDirective = new GraphQLCustomDirective({
name: 'duplicate',
description: 'duplicate the string sperating them with space',
Expand All @@ -38,24 +58,26 @@ describe('GraphQLCustomDirective', () => {
type: GraphQLInt,
description: 'the times to duplicate the string',
},
opts: {
type: new GraphQLList(TestOption),
description: 'additional options',
}
},
resolve: function(resolve, source, {by}, schema, info) {
resolve: function(resolve, source, {by, opts}, schema, info) {
return resolve().then(result => {
if (!result) {
return result;
}

let times = [];

for (let i = 0; i < (by || 2); i++) {
times.push(result);
if (result) {
result = doDuplicate(result, by);
if (opts) {
for (let i = 0; i < opts.length; i++) {
result = doDuplicate(result, opts[i].againBy);
}
}
}

return times.join(' ');
return result;
});
},
});
GraphQLTestDirectiveTrows = new GraphQLCustomDirective({
GraphQLTestDirectiveThrows = new GraphQLCustomDirective({
name: 'throws',
description: 'throws an error after promise is resolved',
locations: [DirectiveLocation.FIELD],
Expand Down Expand Up @@ -141,6 +163,30 @@ describe('GraphQLCustomDirective', () => {
testEqual({directives, query, schema, input, expected, done});
});

it('expected directive to handle complex argument types', done => {
const query = `{ value @duplicate(opts: [{againBy:2} {againBy:2}]) }`,
schema = `type Query { value: String } schema { query: Query }`,
input = {value: 'test'},
directives = [GraphQLTestDirective],
expected = {value: 'test test test test test test test test'};

testEqual({directives, query, schema, input, expected, done});
});

it('expected directive to handle multiple arguments and variables', done => {
const query = `
query TestVariables($by: Int) {
value @duplicate(by: $by, opts: [{againBy:2}])
}`,
schema = 'type Query { value(input: Int): String } schema { query: Query }',
input = {value: 'test'},
variables = {by: 3},
directives = [GraphQLTestDirective],
expected = {value: 'test test test test test test'};

testEqual({schema, directives, query, input, variables, expected, done});
});

it('expected directive to alter execution of graphql and result null', done => {
const query = `{ value @duplicate }`,
directives = [GraphQLTestDirective],
Expand All @@ -155,7 +201,7 @@ describe('GraphQLCustomDirective', () => {
passServer = true,
directives = [
GraphQLTestDirective,
GraphQLTestDirectiveTrows,
GraphQLTestDirectiveThrows,
GraphQLTestDirectiveCatch,
];

Expand Down
5 changes: 4 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const _runQuery = function({
passServer = false,
done,
context,
variables,
}) {
let executionSchema = buildSchema(schema || DEFAULT_TEST_SCHEMA);

Expand All @@ -43,14 +44,15 @@ const _runQuery = function({

applySchemaCustomDirectives(executionSchema);

return graphql(executionSchema, query, input, context);
return graphql(executionSchema, query, input, context, variables);
};

exports.testEqual = function({
directives,
query,
schema,
input,
variables,
passServer = false,
expected,
done,
Expand All @@ -61,6 +63,7 @@ exports.testEqual = function({
query,
schema,
input,
variables,
passServer,
done,
context,
Expand Down