Primeros pasos implementando GraphQL
¿Qué es GraphQL?
Lenguaje de consultas
Esquema
GraphQL
Describe la estructura de los tipos, consultas y mutaciones
type Character {
name: String!
appearsIn: [Episode]!
}
Tipos GraphQL
Para describir los tipos de datos que podemos manejar dentro del lenguaje
- Tipo
objeto - Tipo Query
- Tipo Mutation
- Tipo int
- Tipo scalar
type Character { // Object type
name: String!
appearsIn: [Episode]!
}
type Query { Query type
hero(episode: Episode): Character
droid(id: ID!): Droid
}
input ReviewInput {
stars: Int!
commentary: String
}
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
interface Character {
id: String!
name: String
friends: [Character!]!
appearsIn: [Episode]
}
¡Jugar!
¿Qué
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);
¡Gracias!
@mayela0x14
Primeros pasos implementando GraphQL
By Maricela Sanchez
Primeros pasos implementando GraphQL
- 702