Skip to content

GraphQL @mock directive #5836

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

Merged
merged 2 commits into from
Jul 22, 2019
Merged
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
32 changes: 32 additions & 0 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5272,6 +5272,8 @@ describe('ParseGraphQLServer', () => {
hello: String @resolve
hello2: String @resolve(to: "hello")
userEcho(user: _UserFields!): _UserClass! @resolve
hello3: String! @mock(with: "Hello world!")
hello4: _UserClass! @mock(with: { username: "somefolk" })
}
`,
});
Expand Down Expand Up @@ -5357,5 +5359,35 @@ describe('ParseGraphQLServer', () => {

expect(result.data.custom.userEcho.username).toEqual('somefolk');
});

it('can mock a custom query with string', async () => {
const result = await apolloClient.query({
query: gql`
query Hello {
custom {
hello3
}
}
`,
});

expect(result.data.custom.hello3).toEqual('Hello world!');
});

it('can mock a custom query with auto type', async () => {
const result = await apolloClient.query({
query: gql`
query Hello {
custom {
hello4 {
username
}
}
}
`,
});

expect(result.data.custom.hello4.username).toEqual('somefolk');
});
});
});
11 changes: 11 additions & 0 deletions src/GraphQL/loaders/schemaDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FunctionsRouter } from '../../Routers/FunctionsRouter';
export const definitions = gql`
directive @namespace on FIELD_DEFINITION
directive @resolve(to: String) on FIELD_DEFINITION
directive @mock(with: Any!) on FIELD_DEFINITION
`;

const load = parseGraphQLSchema => {
Expand Down Expand Up @@ -46,6 +47,16 @@ const load = parseGraphQLSchema => {
}

parseGraphQLSchema.graphQLSchemaDirectives.resolve = ResolveDirectiveVisitor;

class MockDirectiveVisitor extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
field.resolve = () => {
return this.args.with;
};
}
}

parseGraphQLSchema.graphQLSchemaDirectives.mock = MockDirectiveVisitor;
};

export { load };