Skip to content

Commit

Permalink
'Mutation'
Browse files Browse the repository at this point in the history
  • Loading branch information
jin-xinchen committed Aug 28, 2022
1 parent c9ffcba commit 471045a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions codeqianfen/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const express=require('express');
const {buildSchema}=require('graphql');
const { graphqlHTTP } = require('express-graphql');

const schema=buildSchema(`
type Product {
name: String
upc: String
}
input ProductInput {
name: String
upc: String
}
type Mutation{
addP(input: ProductInput): Product
updateP(id:ID!,input: ProductInput): Product
}
type Query{
product:[Product]
}
`)
const dataDb={};
const root ={
product(){
var arr=[];
for(const k in dataDb){
arr.push(dataDb[k]);
}
return arr;
},
addP({input}){
dataDb[input.name]=input;
return dataDb[input.name];
},
updateP({id, input}){
const pro = Object.assign({},dataDb[id],input);
dataDb[id] = pro;
return pro;
}
}

const app = express();

app.use('/graphql',graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true
}))

app.use(express.static('public'))

app.listen(3000);
// # query {product {name upc}}
// # mutation{addP(input: {name:"egg",upc:"xxxx"}) {name upc}}
// # mutation{addP(input: {name:"milk",upc:"1xxx"}) {name upc}}
// # mutation{updateP(id:"milk",input: {name:"milk",upc:"2ssx2x"}) {name upc}}
// # mutation{updateP(id:"milk",input: {name:"milk",upc:"3ssx33x"}) {name upc}}

0 comments on commit 471045a

Please sign in to comment.