js-graphql-intellij-plugin icon indicating copy to clipboard operation
js-graphql-intellij-plugin copied to clipboard

Laravel/Lighthouse php/Vue backend and frontend in one project errors.

Open asolopovas opened this issue 4 years ago • 5 comments

Hi,

I am using lighthouse-php for my Laravel + Vue project, and I have a small but very annoying issue. The problem is that my project requires 2 schema definitions, one for frontend the other is for the backend. lighthouse-php uses graphql/scheme.graphql schema located in the root folder of Laravel and contains something like that:

"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")

"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

"File upload"
scalar Upload @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Upload")

type Query {
    user: [User] @all
}
#import models/*.gql

The Vue client resides in the resources/js folder with all queries and mutations for the client. If I define my .graphqlconfig like so:

{
    "name": "DEV Schema",
    "schemaPath": "schema-frontend.graphql",
    "extensions": {
        "endpoints": {
            "dev": {
                "url": "https://dev.test/graphql",
                "headers": {
                    "Authorization": "Bearer ${env:PERSONAL_ACCESS_TOKEN}",
                    "user-agent": "JS GraphQL"
                },
                "introspect": false
            }
        }
    }
}

and execute the url it generates schema-frontend.gqaphql in root folder of Laravel.

At this point, my mutations and queries inside the client folder correctly linked with definitions in graphql/schema.graphql that are generated for backend, but with an error tried to redefine existing type error. Because schema-fronted.graphql generated via .graphqlconfig contains same definitions as graphql/schema.graphql.

If I remove schema-frontend.graphql from the project, all client mutations and queries begin to display errors such as Unknown field "name": The parent selection or operation does not resolve to a valid schema type image

Basically, either way with or without generating a schema for the frontend, I end up with errors either in the backend schema or the frontend client queries and mutations, is there a way around it?

Version and Environment Details Operation system: Linux 5.12.5-arch1-1 #1 SMP PREEMPT Wed, 19 May 2021 10:32:40 +0000 x86_64 GNU/Linux IDE name and version: PhpStorm 2021.1.2 Build #PS-211.7142.44, built on April 30, 2021

Plugin version: 2.9.1

asolopovas avatar May 24 '21 16:05 asolopovas

Hi @asolopovas! Have you tried specifying "includes" / "excludes" globs for the configs (docs)? You can have multiple configs or the root one with multiple projects.

vepanimas avatar Jul 21 '21 02:07 vepanimas

Hi @asolopovas, did you fix this issue? How?

jBernavaPrah avatar Dec 04 '21 11:12 jBernavaPrah

Hi @asolopovas, did you fix this issue? How?

Unfortunately I did not solve this problem, excludes/includes doesnt really help, examples provided do not cover my use case.

asolopovas avatar Dec 11 '21 23:12 asolopovas

Hi again!

Found a solution :)

Then on the root of Laravel, create the .grahqlconfig files and put in this content:

{
    "projects": {
        "BE": {
            "excludes": [
                "./storage/app/lighthouse-schema.json"
            ],
            "includes": [
                "graphql/**",
                "schema-directives.graphql"
            ]
        },
        "FE": {
            "excludes": [
                "graphql/**",
                "schema-directives.graphql"
            ],
            "schemaPath": "./storage/app/lighthouse-schema.json"
        }
    }
}

On the GraphQL view on ide, wait until the plugin recognizes the changes.

The only downside of this approach is that every time the schemas on BE (the lighthouse graphql/ files) changes will be needed to run php artisan lighthouse:print-schema --write --json again. This command will create a lighthouse-schema.json with the compiled schema on the default path of storage and will be used by the plugin.

With only these changes, all started to work as aspected.

Let me know if work also for you or I have missed something :D

jBernavaPrah avatar Dec 13 '21 19:12 jBernavaPrah

Thanks to @jBernavaPrah

For the latest version the config looks a little like:

schema: ./storage/app/lighthouse-schema.graphql
include:
    - graphql/**
    - schema-directives.graphql

You also get better performance if you use a native SDL file vs the json. so the artisan command becomes:

php artisan lighthouse:print-schema --write

But you also need to update the schema-directives.graphql every time a change occurs, so you also have to do:

php artisan lighthouse:ide-helper

I've setup a watcher in my IDE to automatically run these 2 commands whenever a file change occurs within my projects graphql folder.

SlyDave avatar Apr 24 '23 15:04 SlyDave