Giter Site home page Giter Site logo

graphgate's Introduction

async-graphql

a high-performance graphql server library that's fully specification compliant

Book中文文档DocsGitHub repositoryCargo package


ci status code coverage Unsafe Rust forbidden Crates.io version docs.rs docs downloads PRs Welcome

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

Static schema

use std::error::Error;

use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Object, Schema};
use async_graphql_poem::*;
use poem::{listener::TcpListener, web::Html, *};

struct Query;

#[Object]
impl Query {
    async fn howdy(&self) -> &'static str {
        "partner"
    }
}

#[handler]
async fn graphiql() -> impl IntoResponse {
    Html(GraphiQLSource::build().finish())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // create the schema
    let schema = Schema::build(Query, EmptyMutation, EmptySubscription).finish();

    // start the http server
    let app = Route::new().at("/", get(graphiql).post(GraphQL::new(schema)));
    println!("GraphiQL: http://localhost:8000");
    Server::new(TcpListener::bind("0.0.0.0:8000"))
        .run(app)
        .await?;
    Ok(())
}

Dynamic schema

Requires the dynamic-schema feature to be enabled.

use std::error::Error;

use async_graphql::{dynamic::*, http::GraphiQLSource};
use async_graphql_poem::*;
use poem::{listener::TcpListener, web::Html, *};

#[handler]
async fn graphiql() -> impl IntoResponse {
    Html(GraphiQLSource::build().finish())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let query = Object::new("Query").field(Field::new(
        "howdy",
        TypeRef::named_nn(TypeRef::STRING),
        |_| FieldFuture::new(async { "partner" }),
    ));

    // create the schema
    let schema = Schema::build(query, None, None).register(query).finish()?;

    // start the http server
    let app = Route::new().at("/", get(graphiql).post(GraphQL::new(schema)));
    println!("GraphiQL: http://localhost:8000");
    Server::new(TcpListener::bind("0.0.0.0:8000"))
        .run(app)
        .await?;
    Ok(())
}

Features

  • Static and dynamic schemas are fully supported
  • Fully supports async/await
  • Type safety
  • Rustfmt friendly (Procedural Macro)
  • Custom scalars
  • Minimal overhead
  • Easy integration (poem, axum, actix-web, tide, warp, rocket ...)
  • Upload files (Multipart request)
  • Subscriptions (WebSocket transport)
  • Custom extensions
  • Error extensions
  • Limit query complexity/depth
  • Batch queries
  • Apollo Persisted Queries
  • Apollo Tracing extension
  • Apollo Federation(v2)

Note: Minimum supported Rust version: 1.75.0 or later

Examples

All examples are in the sub-repository, located in the examples directory.

git submodule update # update the examples repo
cd examples && cargo run --bin [name]

For more information, see the sub-repository README.md.

Integrations

Integrations are what glue async-graphql with your web server, here are provided ones, or you can build your own!

Crate features

This crate offers the following features. Most are not activated by default, except the integrations of GraphiQL (graphiql) and GraphQL Playground (playground):

feature enables
apollo_tracing Enable the Apollo tracing extension.
apollo_persisted_queries Enable the Apollo persisted queries extension.
log Enable the Logger extension.
tracing Enable the Tracing extension.
opentelemetry Enable the OpenTelemetry extension.
unblock Support Asynchronous reader for Upload
bson Integrate with the bson crate.
chrono Integrate with the chrono crate.
chrono-tz Integrate with the chrono-tz crate.
url Integrate with the url crate.
uuid Integrate with the uuid crate.
string_number Enable the StringNumber.
dataloader Support DataLoader.
secrecy Integrate with the secrecy crate.
decimal Integrate with the rust_decimal crate.
bigdecimal Integrate with the bigdecimal crate.
cbor Support for serde_cbor.
smol_str Integrate with the smol_str crate.
hashbrown Integrate with the hashbrown crate.
time Integrate with the time crate.
tokio-sync Integrate with the tokio::sync::RwLock and tokio::sync::Mutex.
fast_chemail Integrate with the fast_chemail crate.
tempfile Save the uploaded content in the temporary file.
dynamic-schema Support dynamic schema
graphiql Enables the GraphiQL IDE integration
playground Enables the GraphQL playground IDE integration

Observability

One of the tools used to monitor your graphql server in production is Apollo Studio. Apollo Studio is a cloud platform that helps you build, monitor, validate, and secure your organization's data graph. Add the extension crate async_graphql_apollo_studio_extension to make this avaliable.

Who's using async-graphql in production?

Community Showcase

  • rust-actix-graphql-sqlx-postgresql Using GraphQL with Rust and Apollo Federation
  • entity-rs A simplistic framework based on TAO, Facebook's distributed database for Social Graph.
  • vimwiki-server Provides graphql server to inspect and manipulate vimwiki files.
  • Diana Diana is a GraphQL system for Rust that's designed to work as simply as possible out of the box, without sacrificing configuration ability.
  • cindythink
  • sudograph

Blog Posts

References

License

Licensed under either of

graphgate's People

Contributors

cawfeecoder avatar dhendrie91 avatar jamesbirtles avatar miaxos avatar sunli829 avatar teh-cmc avatar yatekii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphgate's Issues

When uploading a file through Graphql (using Graphgate), receive "The request's content type is not supported"

Expected Behavior

I expected the request to get forwarded to the backing service and the file to upload successfully

Actual Behavior

GraphGate responds with "The request's content-type is not supported". However, if I hit my service on it's port/address directly, upload is successful.

Steps to Reproduce the Problem

  1. Federate a GraphQL service that accepts file uploads
  2. Stick GraphGate in front
  3. Attempt to upload file through Graphgate

Specifications

  • Version: Latest
  • Platform: linux amd64/darwin arm64
  • Subsystem: N/A

Missing error information

Expected Behavior

Error when accessing service directly:

{
  "data": null,
  "errors": [
    {
      "message": "Some Server Error",
      "locations": [
        {
          "line": 4,
          "column": 7
        }
      ],
      "path": [
        "someMutation"
      ],
      "extensions": {
        "details": "sorry"
      }
    }
  ]
}

I expect this via Graphgate as well

Actual Behavior

Same error via Graphgate:

{
  "data": null,
  "errors": [
    {
      "message": "Some Server Error"
    }
  ]
}

Am I misconfigured something?

Variables used in directive not being passed to downstream services

When using variables in directives like in the following query

query FetchUser($id: ID!, $isNotAdminUser: Boolean!) {
  user(id: $id) {
      id
      firstName
      lastName
      email @skip(if: $isNotAdminUser)
  }
}

The $isNotAdminUser variable is not being passed do downstream federated services but the $id variable is being passed correctly. This only seems to affect variables being used in directives as other variables and variables of different types all seem to work fine.

I did try to hunt down where in graphgate this was occurring and could see some unexpected behaviour in graphgate/crates/planner/src/builder.rs in the referenced_variables function where it seems to check field arguments but not those for directives ? This might not be correct but previous to that function call the correct variables can be seen in debug statements I inserted.

Failed to update schema. error=Redefining the schema is not allowed.

If my understanding is correct, this error is being thrown here because my subgraphs are using a recent version of async-graphql that supports Federation v2, thus their SDLs have a extension for the schema (as they should based on this code).

Expected Behavior

Supergraph should accept SDLs following Apollo Federation v2.

Actual Behavior

Server returns all requests with {"data": null,"errors": [{"message": "Not ready."}]} and logs ERROR Failed to update schema. error=Redefining the schema is not allowed.

Steps to Reproduce the Problem

  1. Clone the repo.
  2. Run docker build -t graphgate-with-examples -f Dockerfile-standalone-demo .. I was getting errors so had to change versions of rust and ubuntu to latest.
  3. Run docker run --name graphgate-issue-27 -d -p 8000:8000 -p 8001:8001 -p 8002:8002 -p 8003:8003 graphgate-with-examples.
  4. Notice that the supergraph runs as expected. Also notice that running query{_service {sdl}} against the individual subgraphs does NOT return extend schema @link...
  5. Run docker stop graphgate-issue-27 && docker rm graphgate-issue-27
  6. Open Cargo.toml and change versions of async-graphql and async-graphql-warp to 5.0.8.
  7. Run docker build -t graphgate-with-examples -f Dockerfile-standalone-demo ..
  8. Run docker run --name graphgate-issue-27 -d -p 8000:8000 -p 8001:8001 -p 8002:8002 -p 8003:8003 graphgate-with-examples.
  9. Notice that the supergraph doesn't work. Also notice that running query{_service {sdl}} against the individual subgraphs returns extend schema @link... (which I think is causing the issue)
  10. Run docker stop graphgate-issue-27 && docker rm graphgate-issue-27 to clean up

Specifications

  • Version: 0.5.1
  • Platform: Mac
  • Subsystem: idk

Migrating from ApolloFederation to GraphGate

In Apollo Federation, we have willSendRequest, didReceiveResponse, and context, which give us control to add forwarding headers, make redirection calls, make compound calls, etc.

I am trying to learn their equivalent in GraphGate.

Required fields not received by federated service from Gateway

Hello!
First, thank you for creating this Gateway.

I'm creating this issue for something I think I've found that's a bug behavior when running GraphGate against some federated services.

For context, I'm using the services under Apollo's Federation Demo
which consist of 4 services: [accounts,reviews,products,inventory].

Here's the query I'm using

query topProds {
  topProducts(first: 1) {
    upc
    inStock
    shippingEstimate
  } 
}

And for context, the shippingEstimate field is declared on the Inventory service extending the Product type and depends on the price and weight field on Product Type.

shippingEstimate: Int @requires(fields: "price weight")

Bug

I noticed the entity resolver for the Inventory service was receiving an object that only has the __typename and upc from graphgate when I selected the shippingEstimate.

 { __typename: 'Product', upc: '1' }

In contrast, when I run the same query but through Apollo's Node Gateway,
the Inventory service receives the price and weight as expected since they are labeled required by shippingEstimate.

{ __typename: 'Product', upc: '1', price: 899, weight: 100 }

Interestingly, I noticed this only happens when I select more than 1 field that come from the Inventory service(inStock).
Ex: When I use this query,

query topProds {
  topProducts(first: 1) {
    upc
    shippingEstimate
  } 
}

graphgate sends the full expected object

{ __typename: 'Product', price: 899, upc: '1', weight: 100 }

Since price and weight are missing, the resolver for shippingEstimate can't resolve.

Expected Behavior

Graphgate should send the Inventory service the price and weight field that are required for the shippingEstimate field.

Actual Behavior

In some situations, the required fields are missing in the object sent to the Federated service.

Steps to Reproduce the Problem

  1. Clone Apollo's federation demo, run npm install and then npm run start-services

  2. Configure graphgate with the following toml config.

    bind = "0.0.0.0:8000"
    
    forward_headers = []
    [jaeger]
    agent_endpoint = "127.0.0.1:6831"
    service_name = "graphgate"
    
    [[services]]
    name = "accounts"              
    addr = "127.0.0.1:4001"      
    query_path = "/"                
    subscribe_path = "/"            
    introspection_path = "/"      
    websocket_path = "/"            
    
    [[services]]
    name = "reviews"
    addr = "127.0.0.1:4002"
    
    
    [[services]]
    name = "products"
    addr = "127.0.0.1:4003"
    
    
    [[services]]
    name = "inventory"
    addr = "127.0.0.1:4004"
    
  3. Run the following query

    query topProds {
      topProducts(first: 1) {
        upc
        inStock
        shippingEstimate
      } 
    }

Your data should have an error.

{
  "data": {
    "topProducts": [
      {
        "upc": "1"
      }
    ]
  },
  "errors": [
    {
      "message": "Int cannot represent non-integer value: NaN",
      "path": [
        "topProducts",
        0,
        0,
        "shippingEstimate"
      ]
    }
  ]
}

Specifications

  • GraphGate Version: 0.5.1
  • Platform: MacOS Catalina
  • Federation Demo Commit Hash: e24d9aca9d7a8fe9490ab53efeddc2ef8152f4a1
  • Node Version: v14.17.0
  • NPM Version: 7.14.0

Additional details.

I captured 3 print outs of Query plans.
The first one is the Query Plan from Apollo's Node Gateway.
_query_plan_node_gateway.txt

This query plan is from GraphGate. I got it by adding this println!("{:#?}", plan); to shared_route_table.rs#169.
In this query plan, the RequiredRef { in the first fetchNode only has upc.
_query_plan_graphgate_missing_fields.txt

And lastly, this GraphGate query plan print out is when I don't query for the inStock field. Notably, the required RequiredRef { now has price and weight, and upc.

_query_plan_graphgate_has_price_weight.txt

Thanks! Please let me know if there's any other info I can provide that can help recreate the bug or invalidate this issue.

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.