mugli / learning-graphql Goto Github PK
View Code? Open in Web Editor NEWAn attempt to learn GraphQL
License: MIT License
An attempt to learn GraphQL
License: MIT License
Since your name and avatar is based on Mowgli have you watched the latest movie http://movies.disney.com/the-jungle-book-2016?
Seems like you don't. Could just make custom types. Are you doing it for brevity?
First of all, thanks a ton for this, it really helped me learn GraphQL. I wanted to suggest an addition in part 7 when you talk about creating custom scalar types for fine-grained validation. What you have there doesn't work at all if a query is using variables instead of arguments. That requires the validation to occur in parseValue
instead of parseLiteral
.
For example, if I have a custom scalar type defined using the docs you have currently (with only parseLiteral
, this will be validated:
mutation {
createUser (email: "invalidemail") { ... }
}
but this will not:
mutation signup ($email: Email!) {
createUser (email: $email) { ... }
}
I'll submit a PR for this, hopefully it will clear things up for anyone using validation on queries with variables.
Hi there,
This is more of a question than an issue but I thought you might have encounter the same problem at some point.
I took the code of the ValidateStringType to create a custom scalar type. Well, I tried just for having an id of 24 characters. And as soon as I use it in a query as an input, I get this:
[TypeError: Cannot read property 'test' of undefined]
I didn't manage to test anything but printing the IdType object. After that, it seems like nothing is executed, neither the query resolve function neither any of the parseValue
, parseLiteral
, serialize
functions.
Any idea of what it could be ?
In your tutorial 7 you can add some more info on creating custom scalar types to do validation like this one,
var ValidateStringType = (params) => {
return new GraphQLScalarType({
name: params.name,
serialize: value => {
return value
},
parseValue: value => {
return value
},
parseLiteral: ast => {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError("Query error: Can only parse strings got a: " + ast.kind, [ast])
}
if (ast.value.length < params.min) {
throw new GraphQLError(`Query error: minimum length of ${params.min} required: `, [ast])
}
if (ast.value.length > params.max){
throw new GraphQLError(`Query error: maximum length is ${params.max}: `, [ast])
}
if(params.regex !== null) {
if(params.regex.test(ast.value)) {
throw new GraphQLError(`Query error: Not a valid ${params.name}: `, [ast])
}
}
return ast.value
}
})
}
A little confused about the GraphQLInterfaceType
documentation.
var NamedType = new GraphQLInterfaceType({
name: 'Named',
fields: {
name: { type: GraphQLString }
}
});
var DogType = new GraphQLObjectType({
name: 'Dog',
interfaces: [ NamedType ],
fields: {
name: { type: GraphQLString },
barks: { type: GraphQLBoolean }
},
isTypeOf: (value) => value instanceof Dog
});
var CatType = new GraphQLObjectType({
name: 'Cat',
interfaces: [ NamedType ],
fields: {
name: { type: GraphQLString },
meows: { type: GraphQLBoolean }
},
isTypeOf: (value) => value instanceof Cat
});
I don't understand why both the CatType
and DogType
have name
as well as have the interfaces: [ NamedType ]
. This seems redundant. What is the purpose of GraphQLInterfaceType
?
I don't think that after 3 years this will be continued, or am i wrong?
Since this is 3 years old already and gets older every day, the information given can already be deprecated but definitely will be at some point.
If you won't continue this work (which i appreciate, really) you should maybe put a big "possible deprecation"-warning topmost and link to the actual GraphQL-documentation :)
This is singularly the best GraphQL resource I know of. I've been recommending it to everyone!
Thanks for putting this together, it's been huge :)
Hey. When I write my code I'm designing it not coding thus I have lots of questions :). Btw, I like your code style. How would you handle nested resources like /projects/:project_id/products/:id
. All tutorials (not just yours) have mutations defined on the root mutation (no nesting). As I described here graphql/graphql-js#221 we could have nested resources the same way as we would do it in REST (nested query). I wonder if that logic is acceptable in graphql
. I would like to see how others handle this case.
Example:
// PUT /projects/:project_id/products/:id
mutation {
// example 1
findProject(id: 1) { // make sure that the project exists and we can access it before mutating data
updateProduct(id: 1, name: "Foo") { // the resolve function receives a valid `project` as the first arg
id
}
}
// example 2
updateProjectProduct(projectId: 10, name: "Product 1") {id}
// example 3
updateProjectProduct(projectId: 10, product: { name: "Product 1"}) {id}
}
Also, should the update
or delete
mutation return an object or null or {status: 'ok'}
or else?
Thank you!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.