Closed semiautomatix closed 5 years ago
I've changed to express-graphql and the problem persists. So it appears to not be an issue with apollo.
I'm using stitching with graphql-tools:
import { mergeSchemas } from 'graphql-tools';
import BinsSchema from './bins';
import CartonsSchema from './cartons';
import PurchaseOrdersSchema from './purchase-orders';
import ProductsSchema from './products';
import SuppliersSchema from './suppliers';
import UsersSchema from './users';
const schema = mergeSchemas({
schemas: [
BinsSchema,
CartonsSchema,
ProductsSchema,
PurchaseOrdersSchema,
SuppliersSchema,
UsersSchema,
],
});
export default schema;
This is the source of the errors, what alternative do I have here?
Ok, the alternative was quite obvious:
import { schemaComposer } from 'graphql-compose';
import * as Bins from './bins';
import * as Cartons from './cartons';
import * as PurchaseOrders from './purchase-orders';
import * as Products from './products';
import * as Suppliers from './suppliers';
import * as Users from './users';
schemaComposer.Query.addFields({
...Bins.queries,
...Cartons.queries,
...Products.queries,
...PurchaseOrders.queries,
...Suppliers.queries,
...Users.queries
});
schemaComposer.Mutation.addFields({
...Bins.mutations,
...Cartons.mutations,
...Products.mutations,
...PurchaseOrders.mutations,
...Suppliers.mutations,
...Users.mutations,
});
const graphqlSchema = schemaComposer.buildSchema();
export default graphqlSchema;
graphql-tools mergeSchemas has known issues with enums, best to avoid!
Thanks for info!
PS. if you already have typeDefs and resolvers in graphql-tools format, then you may use the following methods:
schemaComposer.addTypeDefs(`
type Post {
id: Int!
title: String
votes: Int
}
enum Sort {
ASC
DESC
}
`);
schemaComposer.addResolveMethods({
Query: {
posts: () => Post.find(),
},
Post: {
votes: (post) => Vote.getFor(post.id),
},
});
So stitching graphql-tools schemas and graphql-compose schemas maybe done via schemaComposer methods. With graphql-compose you will able to progromatically stitch types as you wish in more imperative way via addFields, remove and etc... methods.
I encountered exactly the same problem.
Since my project's graphql-tool generated schema has other settings ( dataloader, graphql-middlewares.....) so i cannot merge with graphql-compose-mongoose generated schema by schemaComposer.addTypeDefs
and schemaComposer.addResolveMethods
smoothly.
Instead of trying to merge schema by either mergedSchema
from graphql-tools or schemaComposer.addTypeDefs
and schemaComposer.addResolveMethods
from 'graphl-compose-mongoose
, I eventually start another apollo-server and bindip to only localhost and use introspectSchema
provided by graphql-tools
as below:
const getRemoteMergedSchema = async oldGraphqlSchema => {
const link = new HttpLink({ uri: `http://${host}:${port}`, fetch });
const introspectionResult = await introspectSchema(link);
const remoteGraphqlSchema = await makeRemoteExecutableSchema({
schema: introspectionResult,
link: new HttpLink({ uri: `http://${host}:${port}`, fetch }),
});
const mergedSchema = mergeSchemas({
schemas: [oldGraphqlSchema, remoteGraphqlSchema],
});
return mergedSchema;
};
const startApolloServer = schema =>
new Promise((resolve, reject) => {
const server = new ApolloServer({
schema,
});
// config 'host' for limiting access permission to only localhost machine
server.listen({ port, host }, async err => {
if (err) {
console.log(chalk.red(err));
reject(err);
}
console.log(
chalk.green(`Mongoose schema apollo server is listening on ${port}`),
);
resolve();
});
});
export const startServerAndGetRemoteMergedSchema = async oldGraphqlSchema => {
try {
const autoGenSchema = await getAutoGenSchema();
await startApolloServer(autoGenSchema);
const mergedSchema = await getRemoteMergedSchema(oldGraphqlSchema);
return mergedSchema;
} catch (err) {
console.log(err);
}
};
@JaosnHsieh please open a new issue with small schema demo with midllewares. I'l think how it can be implemented. I will have free time at the end of month.
Globally I'm thinking to make framework from graphql-compose with middlewares (on top of exising middlewares eg shield), auth and DataLoader.
Your example will help me provide better solution for different use cases. Tnx
@JaosnHsieh please open a new issue with small schema demo with midllewares. I'l think how it can be implemented. I will have free time at the end of month.
Globally I'm thinking to make framework from graphql-compose with middlewares (on top of exising middlewares eg shield), auth and DataLoader.
Your example will help me provide better solution for different use cases. Tnx
just opened a new issue and an issue reproduce repo thanks for your reply, awesome project!
Good day
I'm receiving the response below when attempting to sort, I'm unsure whether this falls in the realm of graphql-compose-mongoose or graphql-compose.
GraphQL query:
mongoose model:
graphql-compose-mongoose:
Node server is running apollo-server-express 2.2.2
app.tsx
Any assistance in pointing me in the correct direction would be greatly appreciated.