fdc-graphql

command module
v0.0.0-...-3daa851 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2020 License: GPL-3.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fdc-graphql

A GraphQL prototype for Food Data Central datasets. It is built atop the fdc-api libraries and assumes you have a database built with fdc-ingest. The server schema is built with graphql-go with a playground provided by gqlgen. A demo playground using the FDC Branded Food Products dataset is available at https://go.littlebunch.com/graphql/.

Building

If you are not using the available Docker image at littlebunch/fdcgql, then you need to have Go version 1.11 or higher installed to build and run the server.

Step 1: Clone this repo

Clone the repo into a path other than your $GOPATH:

git clone git@github.com:littlebunch/fdc-graphql.git
Step 2 Configure the database

cd to the fdc-graphql path and configure for your datastore by creating and populating a config.yml:

couchdb:   
  url:  localhost   
  bucket: gnutdata   //default  bucket    
  fts: fd_food  // default full-text index   
  user: <your_user>    
  pwd: <your_password>    

or set these vars in the environment:

COUCHBASE_URL=localhost   
COUCHBASE_BUCKET=gnutdata   
COUCHBASE_FTSINDEX=fd_food   
COUCHBASE_USER=user_name   
COUCHBASE_PWD=user_password   
Step 3: Start the server.
go run main.go -c config.yml -p 8000

Or, run from docker.io (Note: You will need docker installed. You will also need to pass in the Couchbase configuration as environment variables described above. The easiest way to do this is in a file, e.g. 'docker.env', of which a sample is provided in the repo's docker path.) :

docker run --rm -it -p 8000:8000 --env-file=./docker.env littlebunch/fdcgql
Usage

Some queries to run from the playground include:

Query for a food by FDC id:

query  {
   food(id:"356425"){
        fdcId
        foodDescription
        dataSource
        servingSizes{
            nutrientBasis
            servingUnit
            value
        }
        foodGroup{
          description
        }
    } 
}
  
curl -g 'https://go.littlebunch.com/graphql?query={food(id:"356427"){fdcId,foodDescription,company,ingredients,servingSizes{nutrientBasis,servingUnit,value},foodGroup{description}}}'
curl -XPOST -H "Content-type:application-json" https://go.littlebunch.com/graphql -d '{"query":"{food(id:\"356427\"){fdcId,foodDescription,company,ingredients,servingSizes{servingUnit,nutrientBasis,value},foodGroup{description}}}"}'

Browse foods:

{
   foodsBrowse(browse:{page:0,max:50,sort:"foodDescription"}){
        fdcId
        foodDescription
        company
        ingredients
        servingSizes{
           nutrientBasis
           servingUnit
           value
         }
    }
}
curl -g 'https://go.littlebunch.com/graphql?query={foodsBrowse(browse:{page:0,max:50,sort:"foodDescription"}){fdcId,foodDescription,company,ingredients,servingSizes{nutrientBasis,servingUnit,value}}}' 
curl -XPOST -H "Content-type:application/json" https://go.littlebunch.com/graphql -d '{"query":"{foodsBrowse(browse:{page:0,max:50,sort:\"foodDescription\"}){fdcId,foodDescription,company,ingredients,servingSizes{nutrientBasis, servingUnit,value}}}"}'

A list of foods given a list of FDC id's:

{
   foods(fdcids:["344604","344605","344606"]){
        fdcId
        foodDescription
        company
        ingredients
    }
}

Search for foods:

{
   foodsSearch(search:{terms:"broccoli rabe",type:"PHRASE",field:"ingredients"}){
        fdcId
        foodDescription
        company
        ingredients
    }
}
curl -g 'https://go.littlebunch.com/graphql?query={foodsSearch(search:{terms:"broccoli rabe",type:"PHRASE",field:"ingredients"}){fdcId,foodDescription,company,ingredients}}'    

Nutrient data for a food:

{
   food(id:"356425"){
        fdcId
        foodDescription
        dataSource
        servingSizes{
            nutrientBasis
            servingUnit
            value
         }
    }
    nutrientdata(fdcids:["356425"],nutids:[]){
        nutrient
        nutrientno
        value
    }
}
curl -g 'https://go.littlebunch.com/graphql?query={food(id:"356425"){fdcId,foodDescription,dataSource,servingSizes{nutrientBasis,servingUnit,value}}nutrientdata(fdcids:["356425"],nutids:[]){nutrient,nutrientno,value}}'

Nutrient data for an individual nutrient in a food:

{
   food(id:"356425"){
        fdcId
        foodDescription
        dataSource
        servingSizes{
            nutrientBasis
            servingUnit
            value
        }
    } 
    nutrientdata(fdcids:["356425"],nutids:[203,204]){
        nutrient
        nutrientno
        value
    }
}
curl -g 'https://go.littlebunch.com/graphql?query={food(id:"356425"){fdcId,foodDescription,dataSource,servingSizes{nutrientBasis,servingUnit,value}}nutrientdata(fdcids:["356425"],nutids:[203,204]){nutrient,nutrientno,value}}'
curl -XPOST -H "Content-type:application/json" https://go.littlebunch.com/graphql -d '{"query":"{food(id:"356425"){fdcId,foodDescription,dataSource,servingSizes{nutrientBasis,servingUnit,value}}nutrientdata(fdcids:["356425"],nutids:[203,204]){nutrient,nutrientno,value}}"}'

Get a list nutrients from the database:

{
  nutrients{
     nutrientno
     name
     unit
   }
}
curl -g 'https://go.littlebunch.com/graphql?query={nutrients{nutrientno,name,unit}}'
curl -XPOST -H "Content-type:application/json" https://go.littlebunch.com/graphql -d '{"query":"{nutrients{nutrientno,name,unit}}"}'

Documentation

Overview

Package main creates and starts a web server providing a REST endpoint for GraphQL queries

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址