Skip to content

Commit

Permalink
chore(experimentalFragmentVariables): fix test, clarify use case.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Sep 24, 2019
1 parent 797477d commit 98b0986
Showing 1 changed file with 71 additions and 21 deletions.
92 changes: 71 additions & 21 deletions src/test/testSchemaGenerator.ts
Expand Up @@ -12,6 +12,9 @@ import {
ExecutionResult,
GraphQLError,
GraphQLEnumType,
execute,
VariableDefinitionNode,
DocumentNode,
} from 'graphql';
// import { printSchema } from 'graphql';
const { GraphQLJSON } = require('graphql-type-json');
Expand Down Expand Up @@ -2684,32 +2687,16 @@ describe('can specify lexical parser options', () => {
expect(schema.astNode.loc).to.equal(undefined);
});

xit("can specify 'experimentalFragmentVariables' option", () => {
it("can specify 'experimentalFragmentVariables' option", () => {
const typeDefs = `
type Hello {
world(phrase: String): String
}
fragment hello($phrase: String = "world") on Hello {
world(phrase: $phrase)
}
type RootQuery {
hello: Hello
}
schema {
query: RootQuery
type Query {
version: Int
}
`;

const resolvers = {
RootQuery: {
hello() {
return {
world: (phrase: string) => `hello ${phrase}`,
};
},
Query: {
version: () => 1,
},
};

Expand All @@ -2723,6 +2710,69 @@ describe('can specify lexical parser options', () => {
});
}).to.not.throw();
});

// Note that the experimentalFragmentVariables option requires a client side transform
// to hoist the parsed variables into queries, see https://github.com/graphql/graphql-js/pull/1141
// and so this really has nothing to do with schema creation or execution.
it("can use 'experimentalFragmentVariables' option", async () => {
const typeDefs = `
type Query {
hello(phrase: String): String
}
`;

const resolvers = {
Query: {
hello: (root: any, args: any) => {
return `hello ${args.phrase}`;
}
},
};

const jsSchema = makeExecutableSchema({
typeDefs,
resolvers,
});

const query = `
fragment Hello($phrase: String = "world") on Query {
hello(phrase: $phrase)
}
query {
...Hello
}
`;

const parsedQuery = parse(query, { experimentalFragmentVariables: true });

const hoist = (document: DocumentNode) => {
let variableDefs: Array<VariableDefinitionNode> = [];

document.definitions.forEach(def => {
if (def.kind === Kind.FRAGMENT_DEFINITION) {
variableDefs = variableDefs.concat(def.variableDefinitions);
}
});

return {
kind: Kind.DOCUMENT,
definitions: parsedQuery.definitions.map(def => {
return {
...def,
variableDefinitions: variableDefs,
};
}),
};
};

const hoistedQuery = hoist(parsedQuery);

const result = await execute(jsSchema, hoistedQuery);
expect(result.data).to.deep.equal({ hello: 'hello world', });

const result2 = await execute(jsSchema, hoistedQuery, null, null, { phrase: 'world again!' });
expect(result2.data).to.deep.equal({ hello: 'hello world again!', });
});
});

describe('interfaces', () => {
Expand Down

0 comments on commit 98b0986

Please sign in to comment.