Skip to content

Questions about v3.6.0 #191

Closed
Closed
@wravery

Description

@wravery

Thank you @wravery . after changing that modification in my make file. I am getting the output without the error of the HelloClient program. Please check my question also in stackoverflow. In your samples of cppgraphiql doesn't run with dependency of electron-cppgraphql. Other than that. In my project we were using Napi.h. How to rewrite your samples for TodayMock with Napi.h. I am creating HelloMock like TodayMock from samples code placed in project directory. I commented Napi.h codings in "hello.cpp". But in this time i got the following errors

makefile

CFLAGS = -Os -O3 -fPIC -Ofast -std=gnu++17

CC = gcc
CXX = g++

LDFLAGS := \
	-pthread \
	-rdynamic

DEFS := \
	'-DUSING_UV_SHARED=1' \
	'-DUSING_V8_SHARED=1' \
	'-DV8_DEPRECATION_WARNINGS=1' \
	'-DV8_DEPRECATION_WARNINGS' \
	'-DV8_IMMINENT_DEPRECATION_WARNINGS' \
	'-D_LARGEFILE_SOURCE' \
	'-D_FILE_OFFSET_BITS=64' \
	'-DOPENSSL_NO_PINSHARED' \
	'-DOPENSSL_THREADS'	\
	'-D__STDC_FORMAT_MACROS' \
	'-DNAPI_DISABLE_CPP_EXCEPTIONS' \
	'-DBUILDING_NODE_EXTENSION'

# Flags passed to all source files.
GYP_CFLAGS := \
	-frtti \
	-fexceptions \
	-pthread \
	-Wall \
	-Wextra \
	-Wno-unused-parameter \
	-fno-omit-frame-pointer

# Flags passed to only C files.
CFLAGS_C :=

# Flags passed to only C++ files.
CFLAGS_CC := \
	-std=gnu++1y \
	-fexceptions

INC_NAPI := -I/usr/include/node-addon-api
INC_NODE := -I/usr/include/node
# INC := -I./separate
# INC := -I./separate -I../cppgraphqlgen/include -I../cppgraphqlgen/PEGTL/include

# INC_NAPI = -I/usr/lib/node_modules/node-addon-api
# INC_NODE := -I/usr/include/node

CGI_LIB = -lcgicc

# LIBS := /home/jeya/cpp-graphql-projects/cppgraphqlgen/src
LIBS := /usr/local/lib
GRPHQL := -L${LIBS}/libgraphqlclient.a -L${LIBS}/libgraphqljson.a  -L${LIBS}/libgraphqlintrospection.a  -L${LIBS}/libgraphqlservice.a  -L${LIBS}/libgraphqlresponse.a  -L${LIBS}/libgraphqlpeg.a
NOTH_A = nothing.a

# OBJ = nothing.o ./today/sampleQryParse.o ./hello/HelloClient.o ./hello/hello.o

# ENODE = ${NOTH_A} ./today/sampleQryParse.node ./hello/HelloClient.node ./hello/hello.o

OBJ = nothing.o ./hello/HelloClient.o

ENODE = nothing.a ./hello/HelloClient.node ./hello/hello

# GREET_DEMO = ./greet-demo
# CLASS_DEMO = ./class-demo
DEMO := ./today
HELLO := ./hello
target: ${OBJ} ${ENODE}

clean:
	rm -rf *.o *.node *.a
	rm -rf ${DEMO}/*.o ${DEMO}/*.node
	rm -rf ${HELLO}/*.o ${HELLO}/*.node

./today/sampleQryParse.o: ./today/sampleQryParse.cpp
	${CXX} ${CFLAGS} -shared '-DNODE_GYP_MODULE_NAME=sampleQryParse' ${GYP_CFLAGS} ${INC_NODE} ${INC_NAPI} -c $< -o $@

./today/sampleQryParse.node: ./today/sampleQryParse.o
	${CXX} -shared -Wl,-soname=sampleQryParse.node -Wl,--start-group ./today/sampleQryParse.o ${NOTH_A} ${GRPHQL} -Wl,--end-group -o $@

./hello/hello.o: ./hello/hello.cpp
	${CXX} ${CFLAGS} '-DNODE_GYP_MODULE_NAME=hello' ${DEFS} ${GYP_CFLAGS} ${INC_NAPI} ${INC_NODE} -c $< -o $@

./hello/hello.node: ./hello/hello.o
	${CXX} ${CFLAGS} ./hello/hello.o -shared -Wl,-soname=hello.node -Wl,--start-group -lpthread ${GRPHQL} ${NOTH_A} -Wl,--end-group -o $@

./hello/hello: ./hello/hello.cpp
	${CXX} ${CFLAGS} ./hello/hello.cpp -shared -Wl,-soname=hello -Wl,--start-group -lpthread ${GRPHQL} -Wl,--end-group -o $@

# ./hello/HelloClient: ./hello/HelloClient.cpp
# 	${CXX} ${CFLAGS} $< -o $@

./hello/HelloClient.o: ./hello/HelloClient.cpp
	${CXX} ${CFLAGS} '-DNODE_GYP_MODULE_NAME=HelloClient' ${DEFS} ${GYP_CFLAGS} ${INC_NAPI} ${INC_NODE} -c $< -o $@

./hello/HelloClient.node: ./hello/HelloClient.o
	${CXX} ${CFLAGS} -shared -Wl,-soname=HelloClient.node -Wl,--start-group ./hello/HelloClient.o ${GRPHQL} -lpthread ${NOTH_A} -Wl,--end-group -o $@

./today/sampleOne.o: ./today/sample.cpp
	${CXX} ${CFLAGS} -c $< -o $@

./today/sampleOne:
	${CXX} ${CFLAGS} ./today/sample.cpp -shared -Wl,-soname=sampleOne.node -Wl,--start-group ${GRPHQL} -lpthread ${NOTH_A} -Wl,--end-group -o $@

nothing.a: nothing.o
	ar crs nothing.a $<

nothing.o: nothing.c
	${CC} ${LDFLAGS} ${INC_NODE} ${DEFS} -c $< -o $@ 

HelloMock.h

#pragma once

#ifndef HELLOMOCK_H
#define HELLOMOCK_H

#include "HelloSchema.h"

#include <atomic>
#include <stack>

namespace graphql::hello
{
    class Hello;

    class Query
    {
        using helloLoader = std::function<std::vector<std::shared_ptr<Hello>>()>;
        public:
            explicit Query(helloLoader &&getHelloMsg);
            virtual service::FieldResult<std::optional<response::StringType>> getHelloMsg(service::FieldParams &&params) const final;
        private:
            // Lazy load the fields in each query
            void loadMsg(const std::shared_ptr<service::RequestState> &state) const;
            mutable helloLoader _getHelloMsg;
            mutable std::vector<std::shared_ptr<Hello>> _hello;
    };

    class Hello
    {
        public:
            explicit Hello(std::string&& greet);
            virtual service::FieldResult<std::optional<response::StringType>> getGreetMsg() final;
            virtual service::FieldResult<std::optional<response::StringType>> getForceError(
                service::FieldParams &&) const final
            {
                throw std::runtime_error(R"ex(this error was forced)ex");
            }

        private:
            std::string _greet;

        
    };

}

#endif // HELLOMOCK_H

HelloMock.cpp

#include "HelloMock.h"

#include <algorithm>
#include <iostream>

namespace graphql::hello
{

    Hello::Hello(std::string&& greet) : _greet(greet)
    {
    }
    service::FieldResult<std::optional<response::StringType>> Hello::getGreetMsg()
    {
        return _greet;
    }

    Query::Query(helloLoader&& getHelloMsg)
    {
    }
    void Query::loadMsg(const std::shared_ptr<service::RequestState> &state) const
    {
        if (_getHelloMsg)
        {
            _hello = _getHelloMsg();
            _getHelloMsg = nullptr;
        }
    }

    // std::shared_ptr<Hello> Query::findHelloMsg(
    //     const service::FieldParams &params, const response::IdType &id) const
    // {
    //     loadMsg(params.state);

    //     for (const auto &hello : _hello)
    //     {
    //         if (hello->id() == id)
    //         {
    //             return appointment;
    //         }
    //     }

    //     return nullptr;
    // }

} // namespace graphql::hello

hello.cpp

// #include <napi.h>
#include "HelloMock.h"

#include "graphqlservice/JSONResponse.h"
#include <cstdio>
#include <iostream>
#include <iterator>
#include <stdexcept>

using namespace graphql;

// Napi::String parseQuery(const Napi::CallbackInfo &info)
int main(int argc, char **argv)
{
    // Napi::Env env = info.Env();
    /* From HelloMock*/
    response::StringType helloKey;

    std::string fakeHelloKey("hello");
    helloKey.resize(fakeHelloKey.size());
    std::copy(fakeHelloKey.cbegin(), fakeHelloKey.cend(), helloKey.begin());
    
    std::cout << "Created the service..." << std::endl;

    auto query = std::make_shared<hello::Query>(
        [&fakeHelloKey]() -> std::vector<std::shared_ptr<hello::Hello>>
        {
            std::cout << "Called getHelloMsg..." << std::endl;
            return {std::make_shared<hello::Hello>("Hello World!")};
        }
    );
        
    auto service = std::make_shared<hello::Operations>(query);
    std::cout << "Created the service..." << std::endl;

    try
    {
        peg::ast query;
        // std::string qry = info[0].As<std::string>();
        // query = peg::parseString(std::move(qry));
        if (argc > 1)
        {
            query = peg::parseFile(argv[1]);
        }
        else
        {
            std::istream_iterator<char> start{std::cin >> std::noskipws}, end{};
            std::string input{start, end};

            query = peg::parseString(std::move(input));
        }
        if (!query.root)
        {
            std::cerr << "Unknown error!" << std::endl;
            std::cerr << std::endl;
            // return Napi::String::New(env, "1");
            return 1;
        }

        std::cout << "Executing query..." << std::endl;

        // std::stringstream out;
        // out << response::toJSON(service
        //                             ->resolve(nullptr,
        //                                       query,
        //                                       "",
        //                                       response::Value(response::Type::Map))
        //                             .get());
        // std::string res = out.str();
        // return Napi::String::New(env, res);
        std::cout << response::toJSON(service
                                          ->resolve(nullptr,
                                                    query,
                                                    ((argc > 2) ? argv[2] : ""),
                                                    response::Value(response::Type::Map))
                                          .get())
                  << std::endl;
    }
    catch (const std::runtime_error &ex)
    {
        std::cerr << ex.what() << std::endl;
        // return Napi::String::New(env, "1");
        return 1;
    }

    // return Napi::String::New(env, "0");
    return 0;
}

// Napi::Object Init(Napi::Env env, Napi::Object exports)
// {
//     exports.Set("parseQuery", Napi::Function::New(env, parseQuery));
//     return exports;
// }

// NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);

Error:

ar crs nothing.a nothing.o
g++ -Os -O3 -fPIC -Ofast -std=gnu++17 -shared -Wl,-soname=HelloClient.node -Wl,--start-group ./hello/HelloClient.o -L/usr/local/lib/libgraphqlclient.a -L/usr/local/lib/libgraphqljson.a  -L/usr/local/lib/libgraphqlintrospection.a  -L/usr/local/lib/libgraphqlservice.a  -L/usr/local/lib/libgraphqlresponse.a  -L/usr/local/lib/libgraphqlpeg.a -lpthread nothing.a -Wl,--end-group -o hello/HelloClient.node
g++ -Os -O3 -fPIC -Ofast -std=gnu++17 ./hello/hello.cpp -shared -Wl,-soname=hello -Wl,--start-group -lpthread -L/usr/local/lib/libgraphqlclient.a -L/usr/local/lib/libgraphqljson.a  -L/usr/local/lib/libgraphqlintrospection.a  -L/usr/local/lib/libgraphqlservice.a  -L/usr/local/lib/libgraphqlresponse.a  -L/usr/local/lib/libgraphqlpeg.a -Wl,--end-group -o hello/hello
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/c++allocator.h:33,
                 from /usr/include/c++/10/bits/allocator.h:46,
                 from /usr/include/c++/10/memory:64,
                 from /usr/local/include/graphqlservice/GraphQLParse.h:21,
                 from /usr/local/include/graphqlservice/GraphQLService.h:21,
                 from /usr/local/include/graphqlservice/internal/Schema.h:9,
                 from ./hello/HelloSchema.h:11,
                 from ./hello/HelloMock.h:6,
                 from ./hello/hello.cpp:2:
/usr/include/c++/10/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = graphql::hello::Operations; _Args = {std::shared_ptr<graphql::hello::Query>&}; _Tp = graphql::hello::Operations]’:
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = graphql::hello::Operations; _Args = {std::shared_ptr<graphql::hello::Query>&}; _Tp = graphql::hello::Operations; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<graphql::hello::Operations>]’
/usr/include/c++/10/bits/shared_ptr_base.h:551:39:   required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {std::shared_ptr<graphql::hello::Query>&}; _Tp = graphql::hello::Operations; _Alloc = std::allocator<graphql::hello::Operations>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/10/bits/shared_ptr_base.h:682:16:   required from ‘std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = graphql::hello::Operations; _Alloc = std::allocator<graphql::hello::Operations>; _Args = {std::shared_ptr<graphql::hello::Query>&}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/10/bits/shared_ptr_base.h:1371:71:   required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<graphql::hello::Operations>; _Args = {std::shared_ptr<graphql::hello::Query>&}; _Tp = graphql::hello::Operations; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
/usr/include/c++/10/bits/shared_ptr.h:408:59:   required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<graphql::hello::Operations>; _Args = {std::shared_ptr<graphql::hello::Query>&}; _Tp = graphql::hello::Operations]’
/usr/include/c++/10/bits/shared_ptr.h:859:14:   required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = graphql::hello::Operations; _Alloc = std::allocator<graphql::hello::Operations>; _Args = {std::shared_ptr<graphql::hello::Query>&}]’
/usr/include/c++/10/bits/shared_ptr.h:875:39:   required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = graphql::hello::Operations; _Args = {std::shared_ptr<graphql::hello::Query>&}]’
./hello/hello.cpp:33:61:   required from here
/usr/include/c++/10/ext/new_allocator.h:150:4: error: no matching function for call to ‘graphql::hello::Operations::Operations(std::shared_ptr<graphql::hello::Query>&)’
  150 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ./hello/HelloMock.h:6,
                 from ./hello/hello.cpp:2:
./hello/HelloSchema.h:52:11: note: candidate: ‘graphql::hello::Operations::Operations(std::shared_ptr<graphql::hello::object::Query>)’
   52 |  explicit Operations(std::shared_ptr<object::Query> query);
      |           ^~~~~~~~~~
./hello/HelloSchema.h:52:53: note:   no known conversion for argument 1 from ‘shared_ptr<graphql::hello::Query>’ to ‘shared_ptr<graphql::hello::object::Query>’
   52 |  explicit Operations(std::shared_ptr<object::Query> query);
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
make: *** [makefile:89: hello/hello] Error 1

Using Schema

schema.hello.graphql

type Query {
    hello: String
}

query.hello.graphql

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

query {
    hello
}

Excpected output

{"data":{"hello":"Hello World!"}}

How to resolve that error message and how to get the final expected output?

Originally posted by @YJPrakash in #172 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions