Repository navigation
Closed
Labels
questionFurther information is requested
Description
Thank you Bill Avery (@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 &¶ms) 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 ¶ms, 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 Jeyaprakash (@YJPrakash) in #172 (comment)
Metadata
Metadata
Assignees
Labels
questionFurther information is requested
Activity
wravery commentedon Dec 30, 2021
I think the next error is because
class Querydoes not inherit fromobject::Query, which is required in v3.x.You also need to override and match the signature of the virtual field accessors from
object::Queryin your implementation, but it looks like you've done that already.YJPrakash commentedon Jan 3, 2022
After inheriting of
class Queryfromobject::Queryis compiled without errors. Thank you Bill Avery (@wravery).Then I am enabling Napi.h following errors occur. If there any LDflags want to add for
graphql::service.following libraries added for making hello.node
-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. Please take a look at it.hello.cpp
Error:
i) For commented line conversion is not working when enabling
// std::string qry = info[0].As<std::string>();error liketypedef std::__cxx11::basic_string.ii) Node addon program compiled successfully after commented above line. after calling parseQuery function from nodejs error like
Error: ./hello/hello.node: undefined symbol: _ZTIN7graphql7service6ObjectEDetailed error log:
YJPrakash commentedon Jan 3, 2022
wravery commentedon Jan 3, 2022
It looks like you're hitting errors specific to Node/Electron Native Modules now. Building a working native module is complicated and fragile, I've had to spend a lot of time going through V8 headers to unblock major version upgrades of Electron using undocumented
-Ddefines. The safest way I know to do this is to usenpmandcmake-jsinstead of trying to build withmake,cmake-jstakes care of some of the cross-platform and major version incompatibilities for you. Otherwise, you may need to switch togypandelectron-rebuild, which I think are the officially supported build systems for Electron.All that aside, if you just want to pipe queries into an executable and get responses back, take a look at samples/today/sample.cpp. It works in interactive mode (
stdin/stdout), or you can give it a file name for the query you want to execute.YJPrakash commentedon Jan 4, 2022
Yes, correct I am using Node addon API. An error is like an undefined symbol of GraphQL service. So only I am posting the above comment. Today I tried without
Napi.h, my source compiled successfully including CFLAGS-shared -fPIC. But, segmentation-fault occurs when running that program$./hello/hello query.hello.graphql. Without using above mentioned flags I am getting the following errors when compiling. What does it mean about these errors?Please help me. In my project already we tried node-gyp build tool. All other(without node-addon) CPP files were compiled using the make utility. so we found a way to write the Node addon with make utility manually one make process. that's why I am using that above-mentioned make utility.
wravery commentedon Jan 4, 2022
https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable#:~:text=In%20summary%2C%20there%20are%20three%20key%20causes%20of,insufficient%20to%20cause%20the%20error%20on%20their%20own.
YJPrakash commentedon Jan 5, 2022
For Node addon fix:
Node addon using
libgraphqlservice.a. That was recompiled with-fPICflags.Finally, I am found a way to write node addon with
Napi.h.why I am getting the error
"errors":[{"message":"Field error name: hello unknown error: Query::getHello is not implemented"throw from that executable?. But I am implementing thatgetHellofunction inHelloMock.cpp.Please review my code.
HelloMock.h
HelloMock.cpp
hellosample.cpp
It may helpful for implementing
getHellofunction. I will take some time to read about it. Thank you for your help. Bill Avery (@wravery)wravery commentedon Jan 5, 2022
The
getHellomethod should be declared with thefinalkeyword and without thevirtualkeyword, so the compiler can tell you if you didn't override it properly. In this case, I think it will fail to override the stub because the signature doesn't match, you're taking aconst service::FieldParams&&instead ofservice::FieldParams&&. I can't think of a reason why a move/r-value reference would beconst, so you should probably just remove that.The loader function is definitely too complicated for this purpose,
TodayMockuses this for testing purposes (it's a test mock). A more realistic and better structured example is thelearnsample based on the https://graphql.org tutorial, I'd suggest starting from there.This
class Hellodoesn't inherit from anything, so it should not have anyvirtualorfinalmethods. That may be confusinggccand contributing to theundefined reference to `vtableerror.I should have clarified, that's a discussion specifically about the
undefined reference to `vtableerror. It has lots of great troubleshooting information.wravery commentedon Jan 21, 2022
Closing since I haven't heard anything for a couple of weeks. If you're still stuck, feel free to add more comments or open a new issue.