Skip to content

Commit

Permalink
add failing tests for union merges
Browse files Browse the repository at this point in the history
  • Loading branch information
postsa committed Jan 12, 2023
1 parent ea159dd commit dc29326
Showing 1 changed file with 191 additions and 0 deletions.
191 changes: 191 additions & 0 deletions packages/stitch/tests/typeMergingWithUnionsAndInlineFragments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import { stitchingDirectives } from '@graphql-tools/stitching-directives';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { addMocksToSchema } from '@graphql-tools/mock';
import { GraphQLSchema } from 'graphql/type/schema';
import { stitchSchemas } from '@graphql-tools/stitch';
import { graphql } from 'graphql/index';

describe('Unions and Inline Fragments', () => {
const { allStitchingDirectivesTypeDefs, stitchingDirectivesTransformer } = stitchingDirectives();

let studentSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
${allStitchingDirectivesTypeDefs}
type Query {
student(id: String!): Student
}
type Student {
id: String!
name: String!
major: Major!
}
union Major = StubMajor
type StubMajor {
id: String!
}
`,
});

let majorsSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
${allStitchingDirectivesTypeDefs}
type Query {
major(id: String!): Major @merge(keyField: "id")
}
union Major = MajorError | MajorSuccess # Allow for different return values
type MajorError {
id: String!
code: String!
}
type MajorSuccess {
id: String!
name: String!
}
`,
});

let stitchedSchema: GraphQLSchema;

let studentResolvers;

let majorResolvers: any;

afterEach(() => {
jest.resetAllMocks();
});

beforeEach(() => {
studentResolvers = {
Major: {
__resolveType: (_obj: any) => {
return 'StubMajor';
},
},
Query: {
student: async (_source: any, args: { id: string }) => {
return {
id: args.id,
name: 'Bob',
major: {
id: 'COMM.1231',
},
};
},
},
};

majorResolvers = {
Major: {
__resolveType: (obj: any) => {
if (obj.name) return 'MajorSuccess';
if (obj.code) return 'MajorError';
return null;
},
},
Query: {
major: async (
_source: any,
args: { id: string }
): Promise<{ id: string; name: string } | { id: string; code: string }> => {
return {
id: args.id,
name: 'Communications',
};
},
},
};

studentSchema = addMocksToSchema({
schema: studentSchema,
resolvers: studentResolvers,
});
majorsSchema = addMocksToSchema({
schema: majorsSchema,
resolvers: majorResolvers,
});

stitchedSchema = stitchSchemas({
subschemas: [{ schema: studentSchema }, { schema: majorsSchema }],
subschemaConfigTransforms: [stitchingDirectivesTransformer],
resolverValidationOptions: { requireResolversForResolveType: 'ignore' },
});
});
it('handles success', async () => {
const query = /* GraphQL */ `
query {
student(id: "12314241") {
id
name
major {
... on MajorSuccess {
id
name
}
... on MajorError {
id
code
}
}
}
}
`;

const result = await graphql({
schema: stitchedSchema,
source: query,
});
expect(result.errors).toBe(undefined);
expect(result.data).toEqual({
student: {
id: '12314241',
name: 'Bob',
major: {
id: 'COMM.1231',
name: 'Communications',
},
},
});
});
it('error', async () => {
const query = /* GraphQL */ `
query {
student(id: "12314241") {
id
name
major {
... on MajorSuccess {
id
name
}
... on MajorError {
id
code
}
}
}
}
`;
const majorsSpy = jest.spyOn(majorResolvers.Query, 'major');
majorsSpy.mockResolvedValue({ id: 'COMM.1231', code: 'NOT_FOUND' });
const result = await graphql({
schema: stitchedSchema,
source: query,
});
expect(result.errors).toBe(undefined);
expect(result.data).toEqual({
student: {
id: '12314241',
name: 'Bob',
major: {
id: 'COMM.1231',
code: 'NOT_FOUND',
},
},
});
});
});

0 comments on commit dc29326

Please sign in to comment.