Git Product home page Git Product logo

cgql's Introduction

cgql

An implementation of GraphQL Service in C++, made from reference of official specifications! ๐Ÿ˜Ž

Getting Started!

CMake

Prerequisites

Downloading CGQL

  • Cloning using Git
git clone https://github.com/cgql/cgql

Doing this will clone the repository to your current working directory.

  • Using tarball file ( *.tar.gz )
tar -xzvf <file-name>.tar.gz

Download the tarball file from release page and untar it using the above command.

  • Using CGQL

CGQL is a statically linked library for Graphql which you can use on your own project to work with GraphQL in C++. That means you will have to link the library with your project

Suppose you have a folder structure like this:

project/
  |-> src/
  | |-> main.cpp
  | |-> CMakeLists.txt
  |-> lib/
  | |-> cgql/ // this is the cloned / untar-ed cgql folder
  | |-> CMakeLists.txt
  |-> CMakeLists.txt

In the root level CMakeLists.txt add

cmake_minimum_required(VERSION 3.16)

project(next_billion_dollar_startup)

# basic configuration for project like C++ standard, etc...
/.../

add_subdirectory(src) # includes "your" source code
add_subdirectory(lib) # includes libraries for your project

The CMakeLists.txt in lib should contain:

add_subdirectory(cgql)

That's it... CGQL is included in your project like magic!

The CMakeLists.txt in src would contain something this:

project(Source)

add_executable(Source main.cpp)

target_link_libraries(
  Source # your project
  cgqlSource # the static library of cgql ( source )
)

Yeah that's it now you can use CGQL in your project.

An example of executing a basic request

#include <cgql/cgql.h>
#include <cgql/parser/parser.h>

using namespace cgql;

int main() {
  // configuration part
  CgqlInstance test;
  // schema
  test.parseSchema(
    "type Person {"
    "  name: String"
    "  age: Int"
    "}"
    ""
    "type Query {"
    "  person(id: Int): Person"
    "}"
  );

  // resolvers
  ResolverMap resolvers {
    {
      "person",
      [](const Args& args) -> Data {
        Int id = fromVariant<Int>(args["id"]); // argument
        ResultMap p {
          {
            { "name", "cw3dv" },
            { "age", 14 }
          }
        };
        return cgqlSMakePtr<ResultMap>(p);
      }
    }
  };
  // TypeOfMap (used for abstract types)
  TypeOfMap typeOfMap;
  // query
  auto query = parse(
    "{"
    "  cw3dv: person {"
    "    name"
    "    age"
    "  }"
    "}"
  );

  // execution
  auto executionResult = test.executeWith(query, resolvers, typeOfMap);
  printResultMap(*executionResult);
  /* prints result to stdout

  cw3dv
    name cw3dv
    age 14

  */ 
}

Finally run:

cmake -B ./build/
cmake --build ./build/
./build/src/Source

Output:

cw3dv
  name cw3dv
  age 14

And there you go, you have a working GraphQL service... :) More to come like networking stuff, better error management, etc...

cgql's People

Contributors

codingwith3dv avatar

Stargazers

 avatar  avatar  avatar

cgql's Issues

Error handling during parse

Describe the bug
If you pass wrong query string to a parse function, programm will go into infinity loop
How To Reproduce
cgql::parse()
Expected behavior
Some error handling

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.