Giter Site home page Giter Site logo

.dev's Introduction

Hey there, I'm Benjie ๐Ÿ‘‹

I'm a community-funded open source developer working on GraphQL-, PostgreSQL- and Node.js-related projects. Sponsorship from awesome individuals, startups and companies like you helps me spend more time on OSS.

.dev's People

Contributors

benjie avatar jemgillam avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

.dev's Issues

Style guide: add descriptions

Thanks for the style guide โค๏ธ

I was wondering if there were any recommendations how to write descriptions?

The spec says it's markdown but is there anything on top of that to link to other fields or indicate parameters and other things?
Like @link and @param in Javadoc?

Article: GraphQL basics around filtering and there being no "limiting join" to children

https://discord.com/channels/625400653321076807/631489012632125440/1240171683638153276


These queries aren't equivalent. GraphQL operates on a layer-by-layer basis, so you have to execute the top layer fields before you can execute their selection sets, so the first query fetches (presumably, the naming is non-obvious) all orders (without limits), then on those it fetches their id and date and the user of the order (for some reason passes the name parameter to it?).

A more expected query would be something like:

query getOrdersWithUser ($name: "sam") {
  allOrders(forUsername: $name) { 
    id
    date
    User { 
     name
    }
  }
}

However, your latter query better embraces the graph nature of GraphQL: start at the node you know (in this case, a named user) and then traverse the graph to get the data you need (getting the orders for that user).

is graphql smart enough to ...
GraphQL works in a very simple way, resolvers, it's up to your resolvers to be smart. Or use an alternative execution engine that's compatible with GraphQL - you might learn a bit from my talk at GraphQLConf last year: https://www.youtube.com/watch?v=4ao-zjiOGx8&list=PLP1igyLx8foE9SlDLI1Vtlshcon5r1jMJ&index=12

Another way to think of it is:

query getOrdersWithUser ($name: String! = "sam") {
  allOrders { 
    id
    #PLACEHOLDER#
  }
}

This query will return the id of all orders. Querying additional data about each order (e.g. adding more fields in #PLACEHOLDER#) WILL NOT limit the orders returned - you will still get the same list of ids, but now you'll get extra details too.

If you want to limit the orders, you must do that explicitly when you request them:

query getOrdersWithUser ($name: String! = "sam") {
  allOrders(belongingToUsername: $name) { 
    id
    #PLACEHOLDER#
  }
}

GraphQL does not perform any filtering itself, it doesn't even understand the concept of filtering. It passes arguments through to your business logic via resolvers; it's up to your resolvers to implement them. E.g. for this you might have that the resolver for allOrders sees the belongingToUsername argument and thus performs a database query such as select * from orders where user_id = (select id from users where username = $1). This is just an example, GraphQL is independent of the data source you're using, doesn't have to be an SQL database.

e.g. in JS you might do something like:

const resolvers = {
  Query: {
    async allOrders(_, args, context) {
      const { belongingToUsername } = args;
      const { databaseClient } = context;
      const conditions = ['true'];
      if (belongingToUsername) {
        conditions.push(`user_id = (select id from users where username = ${sql.escape(belongingToUsername)})`);
      }
      const query = `
select *
from orders
where (${conditions.join(') AND (')});`;
      return databaseClient.fetchRows(query);
    }
  }
}

Note this is an absolutely terrible resolver, and your resolvers should really hand over to your business logic layer which should perform this logic; I'm just doing this to be helpful to your understanding.

Equally if you're using GraphQL over a REST API your resolver could be something like:

const resolvers = {
  Query: {
    async allOrders(_, args, context) {
      const { belongingToUsername } = args;
      const { apiClient } = context;
      if (belongingToUsername) {
        return apiClient.get(`/users/${belongingToUsername}/orders`);
      } else {
        return apiClient.get(`/orders`);
      }
    }
  }
}

GraphQL doesn't care which of these vastly different things you do - it's down to what resolver you write.

You could even do something which mixes multiple things together:

const resolvers = {
  Query: {
    async allOrders(_, args, context) {
      const { belongingToUsername } = args;
      const { databaseClient, usersService } = context;
      const conditions = ['true'];
      if (belongingToUsername) {
        const user = await usersService.getUserByUsername(belongingToUsername);
        conditions.push(`user_id = ${sql.escape(user.id)}`);
      }
      const query = `
select *
from orders
where (${conditions.join(') AND (')});`;
      return databaseClient.fetchRows(query);
    }
  }
}

Really up to you how you implement your resolvers.

Article: graph traversal

e.g. why parent fields are unaffected by child field arguments.
e.g. how it helps normalized caching
e.g. why it limits (deliberately) what we can do.

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.