A tale of two API's
Dickens!
[ ] 1 or more requests
Yes, we do all this
{
"id" : "1",
"name" : "Walmart",
"contacts" : [
{
"id" : "1",
"name" : "Michael Scott",
"phones" : []
},
{
"id" : "2",
"name" : "Pam Beasley",
"phones" : [{
"id" : "1",
"number" : "1112223344",
"type" : {
"id" : "1",
"description" : "work"
}
}]
}
]
}
{
"retailers" : {
"1" : {
"id" : "1",
"name" : "Walmart",
"contacts" : ["1", "2"]
}
},
"contacts" : {
"1" : {
"id" : "1",
"name" : "Michael Scott",
"phones" : []
},
"2" : {
"id" : "2",
"name" : "Pam Beasley",
"phones" : ["1"]
}
},
"phones" : {
"1" : {
"id" : "1",
"number" : "1112223344",
"type" : "1"
}
},
"phoneTypes" : {
"1" : {
"id" : "1",
"description" : "work"
}
}
}
But it could be so much easier
export default Relay.createContainer(PhoneCard, {
fragments : {
phone : () => Relay.QL`
fragment on Phone {
id,
number,
type {
id,
description
}
}
`
}
});
import express from 'express';
import graphQLHTTP from 'express-graphql';
import { Schema } from '../data/schema';
const GRAPHQL_PORT = 8080;
const graphQLServer = express();
graphQLServer.use('/', graphQLHTTP({
schema : Schema,
pretty : true
}));
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`
));
const GraphQLContact = new GraphQLObjectType({
name : 'Contact',
fields : {
id : globalIdField('Contact'),
name : {
type : GraphQLString,
resolve : (contact) => contact.name
},
email : {
type : GraphQLString,
resolve : (contact) => contact.email
},
phone : {
type : GraphQLPhone,
resolve : (contact) => getPhone(contact.phone)
}
},
interfaces : [nodeInterface]
});
const GraphQLChangeRetailerPrimaryContactMutation = mutationWithClientMutationId({
name : 'ChangeRetailerPrimaryContact',
inputFields : {
retailer : { type : new GraphQLNonNull(GraphQLID) },
contact : { type : new GraphQLNonNull(GraphQLID) }
},
outputFields : {
retailer : {
type : GraphQLRetailer,
resolve : ({ localRetailerId }) => getRetailer(localRetailerId)
}
},
mutateAndGetPayload : ({ retailer, contact }) => {
const retailerId = fromGlobalId(retailer).id;
const contactId = fromGlobalId(contact).id;
setRetailerPrimaryContact(retailerId, contactId);
return { localRetailerId : retailerId };
}
});