graphql-genie
Version:
95 lines (83 loc) • 2.56 kB
text/typescript
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { SchemaLink } from 'apollo-link-schema';
import { GraphQLGenie } from '../index';
const typeDefs = `
interface Submission {
id: ID!
title: String!
text: String
created: DateTime
updated: DateTime
}
type Post implements Submission {
id: ID!
title: String!
text: String
tags: [String]
author: User
likedBy: [User!]
comments: [Comment]
published: Boolean
created: DateTime
updated: DateTime
createdManual: DateTime
updatedManual: DateTime
}
type Comment implements Submission {
id: ID!
title: String!
text: String
author: User
post: Post
approved: Boolean
created: DateTime
updated: DateTime
}
type User {
id: ID!
email: String!
name : String!
address: Address
writtenSubmissions: [Submission]
age: Int
birthday: Date
likedPosts: [Post!]
family: [User]
match: User
orderBy: String
starred: [Star]
}
type Address {
id: ID!
city: String!
user: User
}
union Star = Address | User | Comment | Post
`;
process['testSetup'] = {};
const fortuneOptions = { settings: { enforceLinks: true } };
export const genie = new GraphQLGenie({ typeDefs, fortuneOptions});
export const getClient = (overRideGenie?: GraphQLGenie) => {
let client: ApolloClient<any>;
if (!process['testSetup']['client'] || overRideGenie) {
const genieToCreateClient = overRideGenie || genie;
const schema = genieToCreateClient.getSchema();
const introspectionQueryResultData = <any> genieToCreateClient.getFragmentTypes();
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData
});
client = new ApolloClient({
link: new SchemaLink({ schema: schema }),
cache: new InMemoryCache({fragmentMatcher}),
defaultOptions: {
query: {fetchPolicy: 'no-cache'},
watchQuery: {fetchPolicy: 'no-cache'}
}
});
if (!overRideGenie) {
process['testSetup']['client'] = client;
}
}
return client;
};