Skip to content

Commit 2e0940c

Browse files
davimacedodouglasmuraoka
authored andcommitted
GraphQL @mock directive (#5836)
* Add mock directive * Include tests for @mock directive
1 parent f336cc3 commit 2e0940c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: spec/ParseGraphQLServer.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -5272,6 +5272,8 @@ describe('ParseGraphQLServer', () => {
52725272
hello: String @resolve
52735273
hello2: String @resolve(to: "hello")
52745274
userEcho(user: _UserFields!): _UserClass! @resolve
5275+
hello3: String! @mock(with: "Hello world!")
5276+
hello4: _UserClass! @mock(with: { username: "somefolk" })
52755277
}
52765278
`,
52775279
});
@@ -5357,5 +5359,35 @@ describe('ParseGraphQLServer', () => {
53575359

53585360
expect(result.data.custom.userEcho.username).toEqual('somefolk');
53595361
});
5362+
5363+
it('can mock a custom query with string', async () => {
5364+
const result = await apolloClient.query({
5365+
query: gql`
5366+
query Hello {
5367+
custom {
5368+
hello3
5369+
}
5370+
}
5371+
`,
5372+
});
5373+
5374+
expect(result.data.custom.hello3).toEqual('Hello world!');
5375+
});
5376+
5377+
it('can mock a custom query with auto type', async () => {
5378+
const result = await apolloClient.query({
5379+
query: gql`
5380+
query Hello {
5381+
custom {
5382+
hello4 {
5383+
username
5384+
}
5385+
}
5386+
}
5387+
`,
5388+
});
5389+
5390+
expect(result.data.custom.hello4.username).toEqual('somefolk');
5391+
});
53605392
});
53615393
});

Diff for: src/GraphQL/loaders/schemaDirectives.js

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FunctionsRouter } from '../../Routers/FunctionsRouter';
55
export const definitions = gql`
66
directive @namespace on FIELD_DEFINITION
77
directive @resolve(to: String) on FIELD_DEFINITION
8+
directive @mock(with: Any!) on FIELD_DEFINITION
89
`;
910

1011
const load = parseGraphQLSchema => {
@@ -46,6 +47,16 @@ const load = parseGraphQLSchema => {
4647
}
4748

4849
parseGraphQLSchema.graphQLSchemaDirectives.resolve = ResolveDirectiveVisitor;
50+
51+
class MockDirectiveVisitor extends SchemaDirectiveVisitor {
52+
visitFieldDefinition(field) {
53+
field.resolve = () => {
54+
return this.args.with;
55+
};
56+
}
57+
}
58+
59+
parseGraphQLSchema.graphQLSchemaDirectives.mock = MockDirectiveVisitor;
4960
};
5061

5162
export { load };

0 commit comments

Comments
 (0)