Code Monkey home page Code Monkey logo

Comments (2)

sogko avatar sogko commented on July 21, 2024

Hi @robinskumar73

Thanks your taking your time to explore and use graphql-go, we appreciate it very much.

I've added comments to your original stack overflow question, I'll elaborate it more here


As, I am from Angular Background this Relay concept are still very confusing to me. How relay works in facilitating the communication between clients and server.

Yeah you are right that Relay has some concepts that would take a while for one to wrap their head around.

(I had planned to write a whole series for GraphQL+Relay, but unfortunately I had to put that on hold for the moment, I do want to go back in and just bang out a whole bunch of them out)

You can use GraphQL without Relay. Relay just works really well with GraphQL + React stack. it's just a library that helps with certain use-cases that otherwise you would have to implement yourself, namely:

  • co-location of GraphQL data with your React code (similar to how you can co-locate your HTML markup with React code
  • Imagine that each of your React components declare its own GraphQL query for exactly what it needs. Now imagine that you have a bigger component that is composed of these little components. Relay helps to merge and figure out one big GraphQL query that would allow you to get all the data to render your React component in one call
  • Relay helps with tracking mutations done to your data objects that you retrieved from your GraphQL endpoints. Thats where it introduces these new concepts of Nodes, Edges, Connections, Global IDs (that are not part of GraphQL). Relay essentially assign store your data as a graph and gives you extra goodies like optimistic updates and data consistency.

Regarding paginations:
Paginations are not part of GraphQL specs but it is part of the Relay library as Connections.

But you can implement paginations without Relay by defining input arguments for your GraphQL objects.
For example:

friendType := graphql.NewObject(...) // define your `friend` type
query := graphql.NewObject(&graphql.ObjectConfig{
        Name: "Query",
        Fields: graphql.Fields{
            "friends": &graphql.Field{
                Type: graphql.NewList(friendType),
                Args: graphql.FieldConfigArgument{
                    "first": &graphql.ArgumentConfig{
                        Type: graphql.Int,
                    },
                },
                Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                    // get and type assert argument
                    first, _ := p.Args["first"].(int)

                    // add logic here to retrieve and paginate your data based on the `first` argument
                    return getFriends(first)
                },
            },
        },
    });

Btw, the example GraphQL query you have seems a bit wonky. It should be

friends(first: 1) { // instead of friends.first(1)
  cursor
  node {
    name
  }
}

where friends object accepts first input argument of type GraphQLInt.

I will try to find time to create an example for pagination for graphql-go, seems like it would be very useful for a lot of people.
(Or if anyone wants to contribute to the existing examples folder, PRs are more than welcomed =))

Do share more ideas of what you think would be useful, for the benefit of the whole GraphQL community 👍

Cheers!

from graphql.

robinskumar73 avatar robinskumar73 commented on July 21, 2024

@sogko Thanks a lot for your fast response it helped me very much. I would also love to contribute GraphQL community of golang once I build graphQL endpoints for my needs.

from graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.