Skip to content

Instantly share code, notes, and snippets.

@maapteh
Last active March 24, 2021 20:30
Show Gist options
  • Save maapteh/ac8d25be2b44ae175a41259b859d9bfc to your computer and use it in GitHub Desktop.
Save maapteh/ac8d25be2b44ae175a41259b859d9bfc to your computer and use it in GitHub Desktop.
import 'reflect-metadata';
import {
createModule,
testkit,
gql,
} from 'graphql-modules';
export const _hackKeepExtendWorking = {
typeDefs: gql`
type Query {
foo: String!
}
type Mutation {
bar: String!
}
`,
resolvers: {
Query: {
foo() {
return {
name: 'Bar',
};
},
},
Mutation: {
bar() {
return {
name: 'Foo',
};
},
},
},
};
describe('testModule', () => {
const ListModule = createModule({
id: 'list',
typeDefs: gql`
extend type Query {
list: List!
}
type List {
id: ID!
title: String!
}
`,
resolvers: {
Query: {
list() {
return {
id: '123-456-789',
title: 'foo'
};
},
}
}
});
const ProductModule = createModule({
id: 'product',
typeDefs: gql`
extend type Query {
product: Product!
}
type Product {
id: ID!
}
# this module extends on list
extend type List {
product: Product!
}
`,
resolvers: {
Query: {
product() {
return {
id: '42',
};
},
},
List: {
product: () => {
return {
id: '42',
};
}
}
}
});
test('should not throw error (it will)', () => {
expect(() =>
testkit.testModule(ProductModule, {
replaceExtensions: true,
inheritTypeDefs: [
ListModule,
],
})
).not.toThrow();
});
test('with hack i can make it work', async () => {
const app = testkit.testModule(ProductModule, {
..._hackKeepExtendWorking,
inheritTypeDefs: [
ListModule,
],
})
const result = await testkit.execute(app, {
document: gql`
query product {
product {
id
}
}
`
});
expect(result.data.product.id).toBe('42');
});
});
@maapteh
Copy link
Author

maapteh commented Mar 24, 2021

results in:

    Error name:    "Error"
    Error message: "There can be only one type named \"List\"."

what i need as testkit option is to replace extensions on Queries/Mutations/Subscriptions but leave extending on eachother types in place. Else i have to hack it to run it, now i add a query and mutation so the extension gives no problems.

@maapteh
Copy link
Author

maapteh commented Mar 24, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment