purescript-graphql icon indicating copy to clipboard operation
purescript-graphql copied to clipboard

Cannot use in REPL

Open rndnoise opened this issue 7 years ago • 2 comments

This only happens with pulp repl. There are no problems using pulp run, but I haven't tried other methods of running the code, like pulp build -O example.js && node example.js.

$ pulp init
$ bower install purescript-graphql --save
$ purs repl
> import Prelude
> import GraphQL        as Q
> import GraphQL.Type   as Q
> import Data.Maybe     (Maybe(..))
> import Data.Either    (Either(..))
> import Effect         (Effect)
> import Effect.Console as C
> import Effect.Aff     (runAff_)
> import Global.Unsafe  (unsafeStringify)
> :paste
schema :: Q.Schema Unit Unit
schema = Q.schema queryType Nothing

queryType :: Q.ObjectType Unit (Maybe Unit)
queryType = Q.objectType
  "Query"
  (Just "This is the root query operation type")
  { helloWorld: Q.field'
      (Q.nonNull Q.string)
      (Just "Basic hello world query")
      \_ _ -> pure "hello world" }

main :: Effect Unit
main = runAff_ log $
  Q.graphql schema "{ helloWorld }" unit unit Nothing Nothing
  where
    log (Left ex) = C.error (show ex)
    log (Right x) = C.log (unsafeStringify x)

^D
> main
/Users/rndnoise/example/.psci_modules/node_modules/GraphQL.Type/foreign.js:21
    return new G.GraphQLNonNull(type);
           ^

TypeError: G.GraphQLNonNull is not a constructor
    at /Users/rndnoise/example/.psci_modules/node_modules/GraphQL.Type/foreign.js:21:12
    ...

I'm not sure why this happens but I suspect it might be due to the PureScript module's name GraphQL being the same as the JavaScript library graphql, and that pulp repl might not be setting up the same library paths as pulp run.

When I add this to one of the foreign.js files, var G = require("graphql"); console.warn(G); the output is { graphql: [Function: graphql] }. That looks like the PureScript module, rather than the JavaScript module.

This happens whether or not I have graphql installed via npm, so it seems require("graphql") is actually loading the PureScript GraphQL module rather than the Facebook library. Doing npm install graphql --save doesn't seem to change anything.

rndnoise avatar Dec 02 '18 23:12 rndnoise

I was able to reproduce this issue on Windows. I am not sure if this should necessarily be considered a bug in this library or in PSCI. I guess I just chose the name not very fortunate...

I think the following happens: So it seems like PSCI creates a node_modules folder in .psci_modules. Inside it then tries to resolve the module graphql which on case sensitive file systems should not find a match in the _ current folder's_ modules so it looks one level higher in the parent directory where our project's node modules can be found and returns the correct module. On windows however it finds the PureScript module. I will check on my Mac tomorrow if the bug is not reproducable there.

Maybe PSCI could switch to relative paths for all purescript imports...

hendrikniemann avatar Dec 04 '18 23:12 hendrikniemann

Oh good point about the case-sensitive filesystem. I ran into the problem on macOS with a case-insensitive filesystem. While I don't have a different filesystem to test with, it seems to be the likely cause assuming your Windows filesystem is case-sensitive.

I can't find any options to configure PSCi's search path and running NODE_PATH=node_modules pulp repl didn't seem to have any effect. I don't know enough about the differences between how purs is invoked via pulp run or pulp repl to know why one works and the other doesn't.

rndnoise avatar Dec 05 '18 01:12 rndnoise

v2 does not use any FFI at all, so this problem should be fixed

hendrikniemann avatar Jul 10 '23 20:07 hendrikniemann