我有以下对象:
tabs: {
<id>: { // this id will be a dinamic id and everytime it will contain UUID
title: "",
body: ""
}
}我想知道如何为这个随机ID和解析器生成一个模式,有什么想法吗?
发布于 2020-07-15 05:52:13
这些天我也在研究类似的东西。
我从我的研究中改编了一些代码。如果这有帮助,请告诉我。
const { ApolloServer } = require("apollo-server");
const { generate } = require("shortid");
const typeDefs = `
type Tab {
id: ID!
title: String!
body: String!
}
type Query {
allTabs: [Tab!]!
}
type Mutation {
addTab(title: String! body: String!): Tab
}
`;
let tabs = [
{
id: "2WEKaVNO",
title: "Title 1",
body: "Body 1"
}
];
const resolvers = {
Query: {
allTabs: () => tabs
},
Mutation: {
addTab: (parent, { title, body }) => {
let newTab = {
id: generate(),
title,
body
};
tabs = [...tabs, newTab];
return newTab;
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server
.listen()
.then(({ url }) => `GraphQL server listening on ${url}`)
.then(console.log)
.catch(console.error);```https://stackoverflow.com/questions/62893285
复制相似问题