Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
serialize(value) {
return new Date(value).toISOString()
},
parseValue(value) {
return new Date(value);
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new TypeError(`Date cannot represent non string type`);
}
return new Date(ast.value);
}
})
}
});
const schemaB = makeExecutableSchema({
typeDefs: [`type Query { b: String }`],
});
const schema = mergeSchemas({ schemas: [schemaA, schemaB] });
// original schema A
const { data: dataA } = await graphql({
schema: schemaA,
source: /* GraphQL */` { a } `
});
expect(dataA.a).toEqual(now.toISOString());
// merged schema
const { data } = await graphql({
schema,
it('should merge two valid executable schemas', async () => {
const fooSchema = makeExecutableSchema({
typeDefs: gql`
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => 'FOO'
}
}
});
const barSchema = makeExecutableSchema({
typeDefs: gql`
type Query {
bar: String
}
it('should accept root schema object', () => {
const mergedSchema = mergeTypeDefs(['type RootQuery { f1: String }', 'type RootQuery { f2: String }', 'schema { query: RootQuery }', 'type MyType { field: Int } type RootQuery { f3: MyType }']);
const schema = makeExecutableSchema({
typeDefs: mergedSchema,
});
const queryType = schema.getQueryType();
expect(queryType).toBeDefined();
expect(queryType).not.toBeNull();
expect(queryType.name).toEqual('RootQuery');
});
it('should keep scalars', () => {
const mergedSchema = mergeTypeDefs([buildSchema('scalar UniqueId')]);
expect(print(mergedSchema).indexOf('scalar')).not.toEqual(-1);
const schema = makeExecutableSchema({
typeDefs: mergedSchema,
});
expect(schema.getType('UniqueId')).toBeDefined();
});
it('Should print types correctly if they dont have astNode', () => {
const schema = makeExecutableSchema({
typeDefs: `
scalar JSON
type TestType {
testField: JSON!
}
type Other {
something: String
}
type Query {
test: TestType
other: Other!
}
`,
it('should merge two valid executable schemas with extra typeDefs and resolvers', async () => {
const fooSchema = makeExecutableSchema({
typeDefs: gql`
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: () => 'FOO'
}
}
});
const barSchema = makeExecutableSchema({
typeDefs: gql`
type Query {
bar: String
}
`,
resolvers: {
Query: {
bar: () => 'BAR'
}
}
});
const { errors, data } = await graphql({
schema: mergeSchemas({
schemas: [fooSchema, barSchema],
typeDefs: gql`
type Query {
`
const resolvers = {
Query: {
foo: async () => getFoo(),
},
};
const resolversComposition: ResolversComposerMapping = {
Query: {
foo: next => async () => {
const prevResult = await next();
return getFoo() + prevResult;
}
}
}
const composedResolvers = composeResolvers(resolvers, resolversComposition);
const schema = makeExecutableSchema({
typeDefs,
resolvers: composedResolvers,
});
const result = await execute({
schema,
document: gql`query { foo }`,
});
expect(result.errors).toBeFalsy();
expect(result.data.foo).toBe('FOOFOO');
});
it('should be able to take nested composition objects for subscription resolvers', async () => {
export function mergeSchemas({
schemas,
typeDefs,
resolvers,
resolversComposition,
schemaDirectives,
resolverValidationOptions,
logger,
exclusions,
}: MergeSchemasConfig) {
return makeExecutableSchema({
typeDefs: mergeTypeDefs([
...schemas,
...typeDefs ? asArray(typeDefs) : []
], { exclusions }),
resolvers: composeResolvers(
mergeResolvers([
...schemas.map(schema => extractResolversFromSchema(schema)),
...resolvers ? asArray(resolvers) : []
], { exclusions }),
resolversComposition || {}
),
schemaDirectives,
resolverValidationOptions,
logger
})
}
export async function mergeSchemasAsync({ schemas, typeDefs, resolvers, resolversComposition, schemaDirectives, resolverValidationOptions, logger, ...config }: MergeSchemasConfig) {
const [typeDefsOutput, resolversOutput] = await Promise.all([
mergeTypeDefs([...schemas, ...(typeDefs ? asArray(typeDefs) : [])], config),
Promise.all(schemas.map(async schema => extractResolversFromSchema(schema))).then(extractedResolvers =>
composeResolvers(mergeResolvers([...extractedResolvers, ...(resolvers ? asArray(resolvers) : [])], config), resolversComposition || {})
),
]);
return makeExecutableSchema({
typeDefs: typeDefsOutput,
resolvers: resolversOutput,
schemaDirectives,
resolverValidationOptions,
logger,
});
}
export function mergeSchemas({ schemas, typeDefs, resolvers, resolversComposition, schemaDirectives, resolverValidationOptions, logger, ...config }: MergeSchemasConfig) {
return makeExecutableSchema({
typeDefs: mergeTypeDefs([...schemas, ...(typeDefs ? asArray(typeDefs) : [])], config),
resolvers: composeResolvers(mergeResolvers([...schemas.map(schema => extractResolversFromSchema(schema)), ...(resolvers ? asArray(resolvers) : [])], config), resolversComposition || {}),
schemaDirectives,
resolverValidationOptions,
logger,
});
}