Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mutations = {},
resolvers = {},
reducers = {},
typeDefs = global.globalTypeDefs,
context,
loadingComponent
}: ApolloProps) {
// add crap around mocks
const finalMocks = {
...resolvers,
Mutation: () => mutations || {},
Query: () => queries || {},
};
const schema = makeExecutableSchema({ typeDefs, resolvers: global.globalResolvers });
addMockFunctionsToSchema({
mocks: finalMocks,
schema,
});
const apolloCache = new InMemoryCache(global.__APOLLO_STATE_);
const spyLink = new SpyLink();
const graphqlClient: ApolloClient = new ApolloClient({
cache: apolloCache as any,
context,
link: ApolloLink.from([
spyLink,
new MockLink({ schema }),
]),
loadingComponent
});
private generateSchemaDerivedData(schema: GraphQLSchema): SchemaDerivedData {
const schemaHash = generateSchemaHash(schema!);
const { mocks, mockEntireSchema, extensions: _extensions } = this.config;
if (mocks || (typeof mockEntireSchema !== 'undefined' && mocks !== false)) {
addMockFunctionsToSchema({
schema,
mocks:
typeof mocks === 'boolean' || typeof mocks === 'undefined'
? {}
: mocks,
preserveResolvers:
typeof mockEntireSchema === 'undefined' ? false : !mockEntireSchema,
});
}
const extensions = [];
const schemaIsFederated = this.schemaIsFederated(schema);
const { engine } = this.config;
// Keep this extension second so it wraps everything, except error formatting
if (this.engineReportingAgent) {
export function mockClient(mocks: MockDefinitions): ApolloClient {
const exSchema = makeExecutableSchema({ typeDefs: rawSchema });
addMockFunctionsToSchema({
schema: exSchema,
mocks: mocks as any
});
const netInterface = mockNetworkInterfaceWithSchema({ schema: exSchema });
const client = new ApolloClient({
networkInterface: netInterface
});
return client;
}
handle (context, options = {}) {
if (!this.$schema) {
throw new Error('Schema has not been defined')
}
if (options.mocks) {
addMockFunctionsToSchema({
schema: this.$schema,
preserveResolvers: false
})
}
return graphqlAdonis({
context,
schema: this.$schema
})(context)
}
throw Error('Apollo Server requires either an existing schema or typeDefs');
}
this.schema = graphql_tools_1.makeExecutableSchema({
typeDefs: this.uploadsConfig
? [
index_1.gql `
scalar Upload
`,
].concat(typeDefs)
: typeDefs,
schemaDirectives,
resolvers,
});
}
if (mocks) {
graphql_tools_1.addMockFunctionsToSchema({
schema: this.schema,
preserveResolvers: true,
mocks: typeof mocks === 'boolean' ? {} : mocks,
});
}
this.extensions = [];
if (this.requestOptions.formatError) {
this.extensions.push(() => new formatters_1.FormatErrorExtension(this.requestOptions.formatError, this.requestOptions.debug));
}
if (engine || (engine !== false && process.env.ENGINE_API_KEY)) {
this.engineReportingAgent = new apollo_engine_reporting_1.EngineReportingAgent(engine === true ? {} : engine);
this.extensions.push(() => this.engineReportingAgent.newExtension());
}
if (extensions) {
this.extensions = [...this.extensions, ...extensions];
}
return function createClient(overwriteMocks = {}) {
const mergedMocks = merge({...mockResolvers}, overwriteMocks)
const schema = makeExecutableSchema({ typeDefs })
addMockFunctionsToSchema({
schema,
mocks: mergedMocks
})
const apolloCache = new InMemoryCache(window.__APOLLO_STATE__)
const graphqlClient = new ApolloClient({
cache: apolloCache,
link: new SchemaLink({ schema })
})
return graphqlClient
}
}
const makeSchema = this._federation ? buildFederatedSchema : makeExecutableSchema;
const schema = makeSchema({
typeDefs,
resolvers,
schemaDirectives
});
debug(`created ${this.constructor.name} schema`);
if (this._useMocks) {
debug(`adding mocks, preserveResolvers=${this._preserveResolvers}`);
const mocks = Object.assign({}, this._importedMocks, this._mocks);
addMockFunctionsToSchema({ schema, mocks, preserveResolvers: this._preserveResolvers });
}
this._schema = schema;
return this._schema;
}
export default function initializeApollo({
typeDefs,
mocks,
reducers = {},
reduxMiddlewares = [],
apolloClientOptions = {},
typeResolvers,
context = {},
rootValue = {},
}) {
const schema = makeExecutableSchema({ typeDefs });
if (!!mocks) {
addMockFunctionsToSchema({
schema,
mocks,
});
}
if (!!typeResolvers) {
addResolveFunctionsToSchema(schema, typeResolvers);
}
const graphqlClient = new ApolloClient({
addTypename: true,
networkInterface: {
query(request) {
return graphql(
schema,
print(request.query),
} from 'graphql-tools';
import typeDefs from './definitions';
import resolvers from './resolvers';
import mocks from './mocks';
const isMocked = !process.env.MOVIE_DB_TOKEN ||
!process.env.SFDC_PWD_AND_TOKEN ||
!process.env.SFDC_USERNAME;
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
isMocked && addMockFunctionsToSchema({schema, mocks});
export default schema;