mocked-apollo-link
Using graphql-tools addMockFunctionsToSchema
quickly generate schema mocks for rapid development or testing. Useful if you have a large schema or quries and only care about mocking parts.
Inspriation
-
React Apollo has a mocked link implementation, but that requires manual creation of mocks.
-
LevelUpTuts had a video demonstrating how
MockedProvider
can be used with to genearte mocks based on a local schema
This takes the mocking concept one step further by combining the two techniques and using addMockFunctionsToSchema
to get mocking working quickly.
Testing Example
Given the schema:
type Author { name: String! id: Int! bio: String!} type Query { authors: [Author]}
// AuthorList.js;;; const GET_ALL_AUTHORS = gql` query GET_ALL_AUTHORS { authors { name id } }`; Component { return <Query query=GET_ALL_AUTHORS> { if loading return 'loading...'; if error return `Error `; return dataauthors; } </Query> ; }
// __tests__/Component.js;;; // run an introspection query and save the results someplace; ; const defaultMocks = { return id: 1 name: 'Charles Dickens' ; }; { const MockLink = ; return ;} ;