GraphQL @mock directive (#5836)
* Add mock directive * Include tests for @mock directive
This commit is contained in:
committed by
Douglas Muraoka
parent
f336cc3435
commit
2e0940c996
@@ -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" })
|
||||
}
|
||||
`,
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 => {
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user