在 graphql 中合并不同的模式
merge different schema in graphql
schema.js
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Booking {
_id: ID!
event: Event!
user: User!
}
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
`);
index.js
app.use(
'/graphql',
graphqlHTTP({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true
})
);
我只是想学习 graphql,这是我的第一次,所以我有点困惑上面的查询工作正常但我想要 3 个不同的文件用于预订、用户、事件并将它们合并到一个文件名中index.js 然后导入主 index.js 上面的一个。这是我第一次学习 graphql。任何帮助将不胜感激
您可以使用 graphql-tools
包的 Type definitions (SDL) merging 来合并您的类型定义文件。
This tools merged GraphQL type definitions and schema. It aims to merge all possible types, interfaces, enums and unions, without conflicts.
例如
types/booking.js
:
module.exports = `
type Booking {
_id: ID!
event: Event!
user: User!
}
`;
types/user.js
:
module.exports = `
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
`;
types/event.js
:
module.exports = `
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
`;
types/index.js
:
const { mergeTypeDefs } = require('@graphql-tools/merge');
const bookingType = require('./booking');
const userType = require('./user');
const eventType = require('./event');
const rootTypes = `
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
`;
const types = [bookingType, userType, eventType, rootTypes];
module.exports = mergeTypeDefs(types);
server.js
:
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema, print } = require('graphql');
const typeDefs = require('./types');
const app = express();
const port = 4000;
const schema = buildSchema(print(typeDefs));
console.log(print(typeDefs));
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
}),
);
app.listen(port, () => console.log('Server started at port:', port));
服务器日志:
type Booking {
_id: ID!
event: Event!
user: User!
}
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
Server started at port: 4000
schema.js
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Booking {
_id: ID!
event: Event!
user: User!
}
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
`);
index.js
app.use(
'/graphql',
graphqlHTTP({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true
})
);
我只是想学习 graphql,这是我的第一次,所以我有点困惑上面的查询工作正常但我想要 3 个不同的文件用于预订、用户、事件并将它们合并到一个文件名中index.js 然后导入主 index.js 上面的一个。这是我第一次学习 graphql。任何帮助将不胜感激
您可以使用 graphql-tools
包的 Type definitions (SDL) merging 来合并您的类型定义文件。
This tools merged GraphQL type definitions and schema. It aims to merge all possible types, interfaces, enums and unions, without conflicts.
例如
types/booking.js
:
module.exports = `
type Booking {
_id: ID!
event: Event!
user: User!
}
`;
types/user.js
:
module.exports = `
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
`;
types/event.js
:
module.exports = `
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
`;
types/index.js
:
const { mergeTypeDefs } = require('@graphql-tools/merge');
const bookingType = require('./booking');
const userType = require('./user');
const eventType = require('./event');
const rootTypes = `
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
`;
const types = [bookingType, userType, eventType, rootTypes];
module.exports = mergeTypeDefs(types);
server.js
:
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema, print } = require('graphql');
const typeDefs = require('./types');
const app = express();
const port = 4000;
const schema = buildSchema(print(typeDefs));
console.log(print(typeDefs));
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true,
}),
);
app.listen(port, () => console.log('Server started at port:', port));
服务器日志:
type Booking {
_id: ID!
event: Event!
user: User!
}
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
type Event {
_id: ID!
title: String!
description: String!
price: Float!
creator: User
}
type RootQuery {
events: [Event!]!
users: [User!]!
bookings: [Booking!]!
}
schema {
query: RootQuery
}
Server started at port: 4000