Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec plan failed: required package was not loaded #420

Closed
LeonB opened this issue Nov 9, 2018 · 8 comments
Closed

exec plan failed: required package was not loaded #420

LeonB opened this issue Nov 9, 2018 · 8 comments
Labels
bug Something isn't working

Comments

@LeonB
Copy link

LeonB commented Nov 9, 2018

I have a custom scalar:

models:
  Duration:
    model: bitbucket.org/tim_online/mews-graphql/scalars.Duration
package scalars

import (
	"errors"
	"io"
	"strconv"

	"github.com/99designs/gqlgen/graphql"
	"github.com/senseyeio/duration"
)

func MarshalDuration(d duration.Duration) graphql.Marshaler {
	return graphql.WriterFunc(func(w io.Writer) {
		io.WriteString(w, strconv.Quote(d.String()))
	})
}

func UnmarshalDuration(v interface{}) (duration.Duration, error) {
	if tmpStr, ok := v.(string); ok {
		return duration.ParseISO8601(tmpStr)
	}
	return duration.Duration{}, errors.New("duration should be a ISO8601 formatted string")
}

When running gqlgen I get this error: exec plan failed: required package was not loaded: github.com/senseyeio/duration.Duration

So it looks like it throws an error somewhere in Config.buildObjects(). Building the scalars package works:

go build ./scalars
@mathewbyrne
Copy link
Contributor

mathewbyrne commented Nov 11, 2018

It looks like you have 2 separate definitions for Duration?

    model: bitbucket.org/tim_online/mews-graphql/scalars.Duration

and

"github.com/senseyeio/duration"

Are they supposed to be the same implementation?

@LeonB
Copy link
Author

LeonB commented Nov 12, 2018

@mathewbyrne mews-graphql/scalars.Duration is the marshalling wrapper for Duration:

func MarshalDuration(d duration.Duration) graphql.Marshaler {
	return graphql.WriterFunc(func(w io.Writer) {
		io.WriteString(w, strconv.Quote(d.String()))
	})
}

func UnmarshalDuration(v interface{}) (duration.Duration, error) {
	if tmpStr, ok := v.(string); ok {
		return duration.ParseISO8601(tmpStr)
	}
	return duration.Duration{}, errors.New("duration should be a ISO8601 formatted string")
}

@LeonB
Copy link
Author

LeonB commented Nov 12, 2018

If I change the marshalling wrapper:

package scalars

import (
	"errors"
	"io"
	"strconv"

	"github.com/99designs/gqlgen/graphql"
	"github.com/senseyeio/duration"
)

func MarshalDuration(d duration.Duration) graphql.Marshaler {
	return graphql.WriterFunc(func(w io.Writer) {
		io.WriteString(w, strconv.Quote(d.String()))
	})
}

func UnmarshalDuration(v interface{}) (duration.Duration, error) {
	if tmpStr, ok := v.(string); ok {
		return duration.ParseISO8601(tmpStr)
	}
	return duration.Duration{}, errors.New("duration should be a ISO8601 formatted string")
}

to:

package scalars

import (
	"fmt"
	"io"

	dur "github.com/senseyeio/duration"
)

type Duration struct {
	dur.Duration
}

// UnmarshalGQL implements the graphql.Marshaler interface
func (d *Duration) UnmarshalGQL(v interface{}) error {
	input, ok := v.(string)
	if !ok {
		return fmt.Errorf("input must be a string")
	}

	d2, err := dur.ParseISO8601(input)
	if err == nil {
		*d = Duration{d2}
	}
	return err
}

// MarshalGQL implements the graphql.Marshaler interface
func (d Duration) MarshalGQL(w io.Writer) {
	w.Write([]byte(d.String()))
}

I get no errors.

@LeonB
Copy link
Author

LeonB commented Nov 12, 2018

I've created a minimal testcase here: https://github.com/omniboost/gqlgen-test

$ gorunpkg github.com/99designs/gqlgen -v
exec plan failed: required package was not loaded: golang.org/x/text/language.Region

@LeonB
Copy link
Author

LeonB commented Nov 12, 2018

This fixes it:

	progLoader := newLoader(namedTypes, true)
	progLoader.Import("golang.org/x/text/language")

So I believe what happens: if you're using the Marshalling wrapper methods the external struct (language.Region) is a top level type and gets added to the list of importchecks but it isn't loaded.

@LeonB
Copy link
Author

LeonB commented Nov 12, 2018

Ah, I think this is the solution:

models:
  CountryCode:
    # model: bitbucket.org/tim_online/mews-graphql/scalars.CountryCode
    model: golang.org/x/text/language.Region

@vektah vektah added the bug Something isn't working label Nov 16, 2018
@LeonB
Copy link
Author

LeonB commented Nov 19, 2018

Looks like it was my fault :(

When I changed type CountryCode to scalar CountryCode in my schema.graphql it worked.

@LeonB LeonB closed this as completed Nov 19, 2018
@rwrz
Copy link
Contributor

rwrz commented Sep 19, 2023

#2800
in case anyone wants a duration implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants