const express = require("express");
const { gql, ApolloServer } = require("apollo-server-express");
const heroes = [
{ id: 1000, name: "bunny" },
{ id: 2000, name: "ram" }
];
const typeDefs = gql`
type Query {
heroes: [Hero]
getHero(id: Int): Hero
}
type Hero {
id: Int
name: String
}
`;
const resolvers = {
Query: {
heroes: () => heroes,
getHero: (_, args, context) => heroes.find(e => e === args.id)
}
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log("http://localhost:4000" + server.graphqlPath)
);
getHero: (_, args, context) => heroes.find(e => e === args.id)
{
getHero(id:1000){
id
name
}
}
export const resolvers = {
User: {
hasLocations(obj, args, context, info) {
// -- count the locations and set to false / true
return true;
}
}
};
hasLocation
field?
relay
library. I created a SO post here: https://stackoverflow.com/questions/61684910/graphql-go-relay-the-base64-hash-of-my-node-id-is-not-what-i-expect. If anybody can help, would really appreciate it!
func AsyncResolve(fn graphql.FieldResolveFn) graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
type result struct {
data interface{}
err error
}
ch := make(chan *result, 1)
go func() {
defer close(ch)
data, err := fn(p)
ch <- &result{data: data, err: err}
}()
return func() (interface{}, error) {
r := <-ch
return r.data, r.err
}, nil
}
}
With the above function, you could now easily do stuff like the following:
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: AsyncResolve(func(p graphql.ResolveParams) (interface{}, error) {
fmt.Println("hello")
time.Sleep(5 * time.Second)
fmt.Println("hello done")
return "hello", nil
}),
},
"world": &graphql.Field{
Type: graphql.String,
Resolve: AsyncResolve(func(p graphql.ResolveParams) (interface{}, error) {
fmt.Println("world")
time.Sleep(3 * time.Second)
fmt.Println("world done")
return "world", nil
}),
},
}
and voila, you've got resolvers that run concurrently :)
fields must be an object with field names as keys or a function which return such an object
var getResultat =&graphql.Field{
Type : ????????????
Args : graphql.FieldConfigArgument{
"type": &graphql.ArgumentConfig{
Type : strings,
}
"code": &graphql.ArgumentConfig{
Type : strings,
}
}
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
filter := bson.M{}
if T := p.Args["type"]; T != nil {
filter["type"] = T.(string)
return (&models.A{}).listA(filter)
}
if B := p.Args["isHappy"]; B != nil {
filter["isbn"] = I.(string)
}
return (&models.Book{}).listB(filter)
},
}
dddw
anyone know a good manual to deploy graphql with nginx ssl termination ?Hi All,
How can I write Graphql Types for an Array of Objects inside Object inside Object inside root Object?
Ex: {
trigger_info : {
trigger_data: {
[
{
trigger_id : "001",
trigger_date : "01/02/20",
trigger_user : "John",
trigger_description : "xxxxxxxx"
},
{
trigger_id : "002",
trigger_date : "01/03/20",
trigger_user : "Paul",
trigger_description : "xxxxxxxx"
},
{
trigger_id : "003",
trigger_date : "01/05/20",
trigger_user : "Finch",
trigger_description : "xxxxxxxx"
}
]
}
}
}
Can Anyone please let me know the model for this above example. If you can, It would be more helpful for me. Thanks a lot in advance.
@silentnull @patcito@gmail.com @mail@matthiasloibl.com
Hey all, looking for some assistance with nested object OR subdocuments, I'm not sure which use case is correct. I've got Graphql in front of MongoDB. The object in my schema is not flat, it has 1 property that is an object. I need to be able to select object based on values in the properties of this child object.
Does anyone know of a good example of this that they could link me to?
Mongo data:
{
car: {
color: "red",
engine: { hp: 10 }
}
}
Graph Schema I tried, seams invalid.
type Car {
color: String
engine: {
hp: Int
}
}
Other schema I tried, compiles but I can't figure out how to filter on the child object:
type Engine {
hp: Int
}
type Car {
color: String
engine: Engine
}
trajanus
It looks like just what I need but wanted to solicit any impressions or tips from those who have already used it.