我已经开始在我的服务器上编写支持GraphQL连接的架构,但是,我不了解如何使用响应的数据填充连接。
我使用单根查询设计。我不确定这是否可以支持更复杂的结构,例如连接和分页,或者我是否需要使用GraphQLObjectType
。
这是我的graphql服务器的样子:
import express from 'express';
import cors from 'cors';
import graphqlHTTP from 'express-graphql';
// Functions that call legacy REST endpoints
import { accounts } from './api/accounts';
import { user } from './api/users';
import schema from './schema';
// GraphQL API
const root = {
user,
accounts,
};
const app = express();
app.use(cors());
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true
}));
这是我的架构:
import {
buildSchema,
} from 'graphql';
export default buildSchema(`
type PageInfo {
startCursor: String
endCursor: String
hasNextPage: Boolean
}
type User {
firstName: String
lastName: String
personId: Int
accountsConnection(
first: Int,
after: String,
last: Int,
before: String
): UserAccountsConnection
}
type UserAccountsConnection {
pageInfo: PageInfo!
edges: [UserAccountsEdge]
totalCount: Int
}
type UserAccountsEdge {
cursor: String!
node: Account
}
type Account {
accountName: String
accountId: ID
}
type Query {
user(personId: Int): User
users: [User]!
accounts: [Account]!
accountsConnection(first: Int): UserAccountsConnection
}
`);
我为accountsConnection收到空。