eh3rrera / graphql-java-spring-boot-example

Sample GraphQL server implemented with graphql-java and Spring Boot
MIT License
211 stars 151 forks source link

Unable to access GraphQL server and GraphiQL #9

Closed eh3rrera closed 5 years ago

eh3rrera commented 5 years ago

@PatriciaSauer has reported she is not able to access http://localhost:8080/graphql/schema.json and http://localhost:8080/graphiql (404 not found status).

The application doesn't throw any errors at startup.

When a POST request is sent using Postman, a response like the following is sent:

{
  "timestamp": 1542268461797,
  "status": 405,
  "error": "Method Not Allowed",
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
  "message": "Request method 'POST' not supported",
  "path": "/graphql"
}

Does anyone have the same error or have any idea what might be happening? I cannot reproduce it. Thanks.

themanojshukla commented 5 years ago

Patrica, can you share your code that you tried. Long ago when I was exploring GraphQl I also aced similar issue. I don;t remember exact cause and fix to the problem right now, but from the error it is related to binding and configurations. I believe if you share your code I can reproduce and give you possible fixes.

Thanks. :)

PatriciaSauer commented 5 years ago

@eh3rrera Thank you for creating this issue.

@themanojshukla I tried the code of this repository and my code: https://github.com/PatriciaSauer/graphql Thanks in advance.

themanojshukla commented 5 years ago

Hi @eh3rrera , @PatriciaSauer , sorry for so delayed response, I wasn't having time to look into it since my last comment.

But, I found the solution, you can see https://github.com/themanojshukla/graphql this repository, it's your forked repository on my account. I've modified two files,

one in pom.xml, added below dependency, so that GraphiQL tool can be used to understand better. // optional change

`

com.graphql-java
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>4.3.0</version>

`

and second one is in GraphqlApplication.java // Only this change alone would fix your issue.

Added a ServletRegistrationBean to map /graphql path. Now, you can access it via http://localhost:8080/graphiql (from Graphiql Interface) or via postman on http://localhost:8080/graphql

Hope this solves your problem.

And, @eh3rrera I think now you can close this issue.

Thanks.

PatriciaSauer commented 5 years ago

Hi @themanojshukla ,

thanks for your reply. Yes, I am now able to access http://localhost:8080/graphiql and http://localhost:8080/graphql :)

Regards, Patricia

eh3rrera commented 5 years ago

Great, thanks a lot @themanojshukla 👍

yacoobgit commented 4 years ago

Hi @themanojshukla ,

thanks for your reply. Yes, I am now able to access http://localhost:8080/graphiql and http://localhost:8080/graphql :)

Regards, Patricia

Could you please send me the latest working code for me?? or share it

yacoobgit commented 4 years ago

I am trying to get sample code using graphiql + spring boot and maven

khteh commented 4 years ago

How to instantiate new SimpleGraphQLHttpServlet which replaces the obsolete SimpleGraphQLServlet?

themanojshukla commented 4 years ago

Hi @themanojshukla , thanks for your reply. Yes, I am now able to access http://localhost:8080/graphiql and http://localhost:8080/graphql :) Regards, Patricia

Could you please send me the latest working code for me?? or share it

How about trying https://github.com/themanojshukla/graphql ?

themanojshukla commented 4 years ago

How to instantiate new SimpleGraphQLHttpServlet which replaces the obsolete SimpleGraphQLServlet?

Hi @khteh,

Have you tried this? https://stackoverflow.com/a/49175785

khteh commented 4 years ago

Tried. Doesn't work. I have also tried the following:

    @Bean 
    ServletRegistrationBean servletRegistrationBean()
    {
        try {
        GraphQLSchema schema  = SchemaParser.newParser()
                .resolvers(bookResolver(authorRepository_), mutation(bookRepository_, authorRepository_), query(bookRepository_, authorRepository_))
                .file("graphql/author.graphqls")
                .file("graphql/book.graphqls")
                .file("graphql/empty.graphqls")
                .build().makeExecutableSchema();
        ExecutionStrategy executionStrategy = new AsyncExecutionStrategy();
        final SimpleGraphQLHttpServlet  servlet = SimpleGraphQLHttpServlet.newBuilder(schema)
        //            .withInstrumentation(factory.getInstrumentations()) How to get the "factory.getInstrumentations() !?!
                    .build(); // This is indicated deprecated in the IDE !?!
        Server svr = config_.getServer();
        Servlet srvlet = svr.getServlet();       
        System.out.println(AppConfig.class.getName() + ".servletRegistrationBean() port: "+svr.getPort() + ", contextPath: " + srvlet.getContextPath());
        ServletRegistrationBean bean = new ServletRegistrationBean(servlet, srvlet.getContextPath());
        return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
khteh commented 4 years ago

The application starts up without error but there is nothing seen from GraphiQL at all. No schema.json generated.

themanojshukla commented 4 years ago

Can you share the repo on github/gitlab/bitbucket anyone u prefer (if it's not confidential) ? I would like to give this a try in my spare time.

khteh commented 4 years ago

https://github.com/khteh/Book

themanojshukla commented 4 years ago

Hey @khteh,

I just checkout your code. It does not contain dependencies from com.graphql-java as mentioned in referred tutorial by @eh3rrera . It's a whole lot of thing that requires fixes in your project and would take ample time which I'm not able to manage. As a suggestion, your pom.xml is literally messed up. You should refactor it and remove lots of unwanted dependencies. And try to stick with the tutorial as much as possible. It would really help.

Furthermore, take a look at this code which solves 404 issue. And then you can try playing with https://stackoverflow.com/a/49175785.

Hope it helps.!

sachink-2020 commented 3 years ago

@eh3rrera I am always having issue while launching the URL "http://localhost:8020/test" for graphql. My server is started.

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Oct 08 11:14:21 EDT 2020 There was an unexpected error (type=Not Found, status=404).

Here are my application.properties spring.application.name=demo-graphql server.port=8020 spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.username=SA spring.datasource.password=

graphql.url=/test graphql.servlet.enabled=true

graphiql.endpoint=/test graphiql.mapping=/graphiql

gradle dependencies plugins { id 'org.springframework.boot' version '2.3.4.RELEASE' id 'io.spring.dependency-management' version '1.0.10.RELEASE' id 'java' }

group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '9'

repositories { mavenCentral() }

dependencies {

compile 'com.graphql-java-kickstart:graphql-java-servlet:7.4.0'
compile 'com.graphql-java:graphql-java-servlet:4.6.1'
implementation 'com.graphql-java:graphql-java:15.0'
implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:7.0.1'
implementation 'com.graphql-java-kickstart:graphql-java-servlet:9.1.0'

implementation 'com.graphql-java-kickstart:graphql-java-tools:5.6.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

}

test { useJUnitPlatform() }

Below is the error as well IdeaProjects\demo-graphql\src\main\java\com\example\demo\exception\GraphQLErrorAdapter.java:31: error: incompatible types: Erro rClassification cannot be converted to ErrorType return error.getErrorType();

How to fix it?

parten23 commented 1 year ago

thanks a lot this helped me...